温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-25 16:22:14 来源:网络 阅读:675 作者:HDDevTeam 栏目:移动开发

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

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

下面我们仔细学习具体的细节。创建一个空的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个选项,PushModalCustom,选中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];

   [actionSheet showInView: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:picker animated:YES];

}


-(void)LocalPhoto

{

UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];

   picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

   picker.delegate = self;

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

   picker.allowsEditing = YES;

   [selfpresentModalViewController:picker animated:YES];

}


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

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

{

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

NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

//当选择的类型是图片

if ([type isEqualToString:@"public.p_w_picpath"])

   {

//先把图片转成NSData

UIImage* p_w_picpath = [info objectForKey:@"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

       [fileManager createDirectoryAtPath: documentsPath withIntermediateDirectories:YESattributes:nilerror:nil];

       [fileManager createFileAtPath:[ documentsPath stringByAppendingString:@"/p_w_picpath.png"] contents:data attributes:nil];

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

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

//关闭相册界面

       [picker dismissModalViewControllerAnimated: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:soundUrl error: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)];

       [button setTitle:@"back"forState:UIControlStateNormal];

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

       [toolBar addSubview:button];

       [moviePlayer.viewaddSubview:toolBar];

       [moviePlayerplay];  

       [moviePlayerstop];

   }

}


-(void)myMovieFinishedCallback:(NSNotification*)notify

{

//视频播放对象

MPMoviePlayerController* theMovie = [notify object];

//销毁播放通知

   [[NSNotificationCenterdefaultCenter] removeObserver:self

name:MPMoviePlayerPlaybackDidFinishNotification

object:theMovie];

   [theMovie.viewremoveFromSuperview];

}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI