资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ios应用程序-图像浏览及音频播放

在这里,主要为大家介绍一下,怎样从相册选取图片并在ImageView中显示出你选择的图片,并且实现简单的音乐和视频播放,下面是运行时的主页面效果图:

成都创新互联公司专业为企业提供贵南网站建设、贵南做网站、贵南网站设计、贵南网站制作等企业网站建设、网页设计与制作、贵南企业网站模板建站服务,十多年贵南做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

ios应用程序-图像浏览及音频播放

下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。

AppDelegate.h应用的代理类这个没什么好说的就是直接打开刚刚创建的新ViewController,在ViewController.h文件里添加如下代码:

#import

#import

#import

//注意这里面引入了很多代理类

@interface ViewController: UIViewController

一.图片的选取

点击导航栏的photo按钮会跳转到photoView页面,至于如何跳转,下面介绍一种简单的跳转方式,则是在storyboard中右键点中photo拖拉到你要跳转的页面,在storyboard segues中有3个选项,Push,Modal和Custom,选中Modal,再次运行,点击photo页面跳转成功。

ios应用程序-图像浏览及音频播放

这时点击导航栏上的camera,会在下方弹出一个UIActionSheet,选择从手机相册获取之后回呈现相册里的图片,根据需求选择完之后会在ImageView中显示出来相应的图片,具体效果图如下:

ios应用程序-图像浏览及音频播放

在项目中添加类文件photoViewController系统自动生成photoViewController.h(头文件)和photoViewController.m(实现文件),在photoViewController.m中从相册选取图片的主要程序如下:

- (IBAction)btnPressed:(id)sender {

UIActionSheet*actionSheet = [[UIActionSheetalloc]

initWithTitle:nil

delegate:self

cancelButtonTitle:@"取消"

destructiveButtonTitle:nil

otherButtonTitles:@"打开照相机",@"从手机相册获取",nil];

   [actionSheetshowInView:self.view];

}

- (void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex

{

if (buttonIndex == actionSheet.cancelButtonIndex)

   {

NSLog(@"取消");

   }

switch (buttonIndex)

   {

case 0:  

//打开照相机拍照

           [selftakePhoto];

break;

case 1:

//打开本地相册

           [selfLocalPhoto];

break;

   }

}

-(void)takePhoto

{

UIImagePickerController *picker=[[UIImagePickerControlleralloc]init];

   picker.sourceType =UIImagePickerControllerSourceTypeCamera;

   picker.delegate=self;

   picker.allowsEditing=YES;

   [selfpresentModalViewController:pickeranimated:YES];

}

-(void)LocalPhoto

{

UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

   picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

   picker.delegate =self;

//设置选择后的图片可被编辑

   picker.allowsEditing =YES;

   [selfpresentModalViewController:pickeranimated:YES];

}

//实现图像选取器控制器的委托

-(void)p_w_picpathPickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info

{

//用UIImagePickerController选择、显示图片或视频

NSString *type = [infoobjectForKey:UIImagePickerControllerMediaType];

//当选择的类型是图片

if ([typeisEqualToString:@"public.p_w_picpath"])

   {

//先把图片转成NSData

UIImage* p_w_picpath = [infoobjectForKey:@"UIImagePickerControllerOriginalImage"];

       NSData *data;

//判断图片是不是JPEG格式的文件

if (UIImagePNGRepresentation(p_w_picpath))

       {

           data =UIImageJPEGRepresentation(p_w_picpath, 1.0);

       }

else

       {

           data =UIImagePNGRepresentation(p_w_picpath);

       }

//图片保存的路径

//指定文件目录这里将图片放在沙盒的documents文件夹中

NSString * documentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];

//文件管理器

NSFileManager *fileManager = [NSFileManagerdefaultManager];

//把刚刚图片转换的data对象拷贝至沙盒中并保存为p_w_picpath.png

       [fileManagercreateDirectoryAtPath: documentsPathwithIntermediateDirectories:YESattributes:nilerror:nil];

       [fileManagercreateFileAtPath:[ documentsPathstringByAppendingString:@"/p_w_picpath.png"]contents:dataattributes:nil];

//得到选择后沙盒中图片的完整路径

filePath = [[NSStringalloc]initWithFormat:@"%@%@", documentsPath,  @"/p_w_picpath.png"];

//关闭相册界面

       [pickerdismissModalViewControllerAnimated:YES];

p_w_picpathView.p_w_picpath = p_w_picpath;

//加在视图中

       [self.viewaddSubview:p_w_picpathView];

   }

}

二.音乐的播放:

音乐由页面中的PickVideo按钮触发播放音频文件,iPhone开发中想要实现音频的播放是很容易的,AVFoundation框架就是Apple本身推荐使用的一种方式。如何引入这个框架,下面为大家详细介绍:

ios应用程序-图像浏览及音频播放

首先,点击工程文件夹,会出现左边的界面,选择我圈起来的那个加号,会出现一系列的框架给你选择,你只需要选择AVFoundation.framework即可。此时,在它的上方就会显示出来,这个框架就添加好了。

播放音乐所需要的程序如下:

- (IBAction)playMp4File:(id)sender {

//    //找到mp3在资源库中的路径文件名称为sound类型为mp3

NSString *soundPath=[[NSBundlemainBundle]pathForResource:@"后来"ofType:@"mp3"];

if(soundPath)

   {

NSURL *soundUrl=[[NSURLalloc]initFileURLWithPath:soundPath];

player=[[AVAudioPlayeralloc]initWithContentsOfURL:soundUrlerror:nil];

//初始化播放器

       [playerprepareToPlay];  

//设置播放循环次数,如果numberOfLoops为负数音频文件就会一直循环播放下去

player.numberOfLoops = -1;  

//设置音频音量 volume的取值范围在 0.0为最小 0.1为最大可以根据自己的情况而设置

player.volume = 0.5f;        

   }

//当player有值的情况下并且没有在播放中开始播放音乐

if (player)  

   {  

if (![playerisPlaying])  

       {

           [playerplay];  

       }

   }

}

- (IBAction)palyStop:(id)sender {

//停止播放声音

if (player) {  

if ([playerisPlaying]) {  

           [playerstop];  

       }

   }  

}

三.视频的播放:

视频的播放由页面中的play MP4 File,play Stop触发,而且播放电影文件时需要注意:ios中可以使用MPMoviePlayerController来播放电影文件这个类定义在MediaPlayer.framework中。同理,添加MediaPlayer.framework框架。

ios应用程序-图像浏览及音频播放

下面是触发pivkVideo时的代码:

- (IBAction)pickVideo:(id)sender

{

NSString *videoPath=[[NSBundlemainBundle]pathForResource:@"犯罪高手" ofType:@"mp4"];

NSLog(@"%@",videoPath);

if(videoPath)

   {

NSURL *videoUrl=[[NSURLalloc]initFileURLWithPath:videoPath];

//视频播放对象

moviePlayer = [[MPMoviePlayerControlleralloc]

initWithContentURL:videoUrl];

//适应屏幕大小,保持宽高比

moviePlayer.controlStyle=MPMovieScalingModeAspectFit;

       [moviePlayer.viewsetFrame:self.view.bounds];

moviePlayer.initialPlaybackTime = -1;

//显示播放/暂停、音量和时间控制

moviePlayer.movieControlMode =MPMovieControlModeDefault;

       [self.viewaddSubview:moviePlayer.view];

//注册一个播放结束的通知

       [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(myMovieFinishedCallback:)                                                    name:MPMoviePlayerPlaybackDidFinishNotification

object:moviePlayer];

UIToolbar *toolBar=[[UIToolbaralloc]initWithFrame:CGRectMake(0, 0, 320, 44)];

UIButton *button=[[UIButtonalloc]initWithFrame:CGRectMake(20, 3, 40, 40)];

       [buttonsetTitle:@"back"forState:UIControlStateNormal];

       [buttonaddTarget:selfaction:@selector(backItem)forControlEvents:UIControlEventTouchUpInside];

       [toolBaraddSubview:button];

       [moviePlayer.viewaddSubview:toolBar];

       [moviePlayerplay];  

       [moviePlayerstop];

   }

}

-(void)myMovieFinishedCallback:(NSNotification*)notify

{

//视频播放对象

MPMoviePlayerController* theMovie = [notifyobject];

//销毁播放通知

   [[NSNotificationCenterdefaultCenter]removeObserver:self

name:MPMoviePlayerPlaybackDidFinishNotification

object:theMovie];

   [theMovie.viewremoveFromSuperview];

}


网站题目:ios应用程序-图像浏览及音频播放
当前网址:http://cdkjz.cn/article/pdschp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220