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

简介:

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

202244564.png

下面我们仔细学习具体的细节。创建一个空的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页面跳转成功。

202323298.png

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

202403342.png

在项目中添加类文件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 = [[UIImagePickerControllerallocinit];

   picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

   picker.delegate = self;

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

   picker.allowsEditing = YES;

   [selfpresentModalViewController:picker animated:YES];

}


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

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

{

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

NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

//当选择的类型是图片

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

   {

//先把图片转成NSData

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

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

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

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

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

//关闭相册界面

       [picker dismissModalViewControllerAnimated:YES];

imageView.image = image;

//加在视图中

       [self.viewaddSubview:imageView];

   }

}

二.音乐的播放:

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

spacer.gif

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

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

- (IBAction)playMp4File:(id)sender {

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

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

if(soundPath)

   {

NSURL *soundUrl=[[NSURLallocinitFileURLWithPath:soundPath];

player=[[AVAudioPlayerallocinitWithContentsOfURL: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框架

202440293.png

下面是触pivkVideo的代

- (IBAction)pickVideo:(id)sender

{

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

NSLog(@"%@",videoPath);


if(videoPath)

   {

NSURL *videoUrl=[[NSURLallocinitFileURLWithPath:videoPath];

//视频播放对象

moviePlayer = [[MPMoviePlayerControlleralloc]

initWithContentURL:videoUrl];

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

moviePlayer.controlStyle=MPMovieScalingModeAspectFit;

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

moviePlayer.initialPlaybackTime = -1;

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

moviePlayer.movieControlMode = MPMovieControlModeDefault;

       [self.viewaddSubview:moviePlayer.view];

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

       [[NSNotificationCenterdefaultCenteraddObserver:selfselector:@selector(myMovieFinishedCallback:)                                                     name:MPMoviePlayerPlaybackDidFinishNotification

object:moviePlayer];

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

UIButton *button=[[UIButtonallocinitWithFrame: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];

//销毁播放通知

   [[NSNotificationCenterdefaultCenterremoveObserver:self

name:MPMoviePlayerPlaybackDidFinishNotification

object:theMovie];

   [theMovie.viewremoveFromSuperview];

}



本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/1218425,如需转载请自行联系原作者

相关文章
|
1月前
|
存储 运维 安全
iOS加固原理与常见措施:保护移动应用程序安全的利器
iOS加固原理与常见措施:保护移动应用程序安全的利器
28 0
|
1月前
|
JSON JavaScript 安全
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
23 1
|
2月前
|
iOS开发 开发者
苹果iOS App Store上架操作流程详解:从开发者账号到应用发布
很多开发者在开发完iOS APP、进行内测后,下一步就面临上架App Store,不过也有很多同学对APP上架App Store的流程不太了解,下面我们来说一下iOS APP上架App Store的具体流程,如有未涉及到的部分,大家可以及时咨询,共同探讨。
|
2月前
|
开发者 iOS开发
iOS应用上架详细图文教程(上)
App Store作为苹果官方的应用商店,审核严格周期长一直让用户头疼不已,很多app都“死”在了审核这一关,那我们就要放弃iOS用户了吗?当然不是!本期我们从iOS app上架流程开始梳理,详细了解下iOS app上架的那些事。
|
2月前
|
Swift iOS开发 开发者
iOS 应用上架流程详解
iOS 应用上架流程详解
|
2月前
|
Android开发 iOS开发 UED
appuploader   iOS 应用自动发布
appuploader   iOS 应用自动发布
|
2天前
|
存储 编解码 JSON
利用SwiftUI构建高效iOS天气应用
【4月更文挑战第21天】 在本文中,我们将深入探讨如何运用SwiftUI框架打造一个响应迅速且用户友好的iOS天气应用程序。我们将重点放在利用SwiftUI的声明式语法简化界面开发,并通过结合Core Location和Networking APIs实现实时天气数据的获取与展示。文章将详细阐述整个开发过程,包括API集成、数据模型设计、用户界面布局以及动态适配不同屏幕尺寸的策略。
|
29天前
|
iOS开发 开发者
iOS移动应用程序的备案与SHA-1值查看
iOS移动应用程序的备案与SHA-1值查看
35 2
|
1月前
|
安全 数据安全/隐私保护 虚拟化
iOS应用加固方案解析:ipa加固安全技术全面评测
iOS应用加固方案解析:ipa加固安全技术全面评测
36 3
|
1月前
|
运维 监控 安全
应用研发平台EMAS常见问题之sophix ios flutter热更新如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
77 0