ios应用程序-图像浏览及音频播放
在这里,主要为大家介绍一下,怎样从相册选取图片并在ImageView中显示出你选择的图片,并且实现简单的音乐和视频播放,下面是运行时的主页面效果图: 下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。 AppDelegate.h应用的代理类这个没什么好说的就是直接打开刚刚创建的新ViewController,在ViewController.h文件里添加如下代码: #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> //注意这里面引入了很多代理类 @interface ViewController: UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,AVAudioPlayerDelegate> 一.图片的选取 点击导航栏的photo按钮会跳转到photoView页面,至于如何跳转,下面介绍一种简单的跳转方式,则是在storyboard中右键点中photo拖拉到你要跳转的页面,在storyboard segues中有3个选项,Push,Modal和Custom,选中Modal,再次运行,点击photo页面跳转成功。 这时点击导航栏上的camera,会在下方弹出一个UIActionSheet,选择从手机相册获取之后回呈现相册里的图片,根据需求选择完之后会在ImageView中显示出来相应的图片,具体效果图如下: 在项目中添加类文件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) { case0: //打开照相机拍照 [selftakePhoto]; break; case1: //打开本地相册 [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)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info { //用UIImagePickerController选择、显示图片或视频 NSString*type = [infoobjectForKey:UIImagePickerControllerMediaType]; //当选择的类型是图片 if([typeisEqualToString:@"public.image"]) { //先把图片转成NSData UIImage* image = [infoobjectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *data; //判断图片是不是JPEG格式的文件 if(UIImagePNGRepresentation(image)) { data =UIImageJPEGRepresentation(image, 1.0); } else { data =UIImagePNGRepresentation(image); } //图片保存的路径 //指定文件目录这里将图片放在沙盒的documents文件夹中 NSString* documentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; //文件管理器 NSFileManager*fileManager = [NSFileManagerdefaultManager]; //把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png [fileManagercreateDirectoryAtPath: documentsPathwithIntermediateDirectories:YESattributes:nilerror:nil]; [fileManagercreateFileAtPath:[ documentsPathstringByAppendingString:@"/image.png"]contents:dataattributes:nil]; //得到选择后沙盒中图片的完整路径 filePath= [[NSStringalloc]initWithFormat:@"%@%@", documentsPath, @"/image.png"]; //关闭相册界面 [pickerdismissModalViewControllerAnimated:YES]; imageView.image= image; //加在视图中 [self.viewaddSubview:imageView]; } } 二.音乐的播放: 音乐由页面中的PickVideo按钮触发播放音频文件,iPhone开发中想要实现音频的播放是很容易的,AVFoundation框架就是Apple本身推荐使用的一种方式。如何引入这个框架,下面为大家详细介绍: 首先,点击工程文件夹,会出现左边的界面,选择我圈起来的那个加号,会出现一系列的框架给你选择,你只需要选择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框架。 下面是触发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]; } 本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/1218425,如需转载请自行联系原作者