一、从图册中获取本地图片和视频

从图册中获取文件,我们使用的是UIImagePickerController,这个类我们在之前的摄像头中使用过,这里是链接:iOS学习笔记27-摄像头,这里我们使用的是它的另外一个功能,那就是从图册中获取图片和视频

基本步骤:
  1. 创建UIImagePickerController对象,并设置代理
  2. 设置拾取源为图册,设置可编辑属性
  3. 如果只是获取图片,就设置图片的媒体类型,如果只是获取视频,就设置视频的媒体类型,如果两者都要获取,就设置两者的媒体类型(实例采用)
  4. 弹出图片选择控制器
  5. 处理图片选择控制器的代理方法
下面是使用实例:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h> @interface ViewController () <UINavigationControllerDelegate,
UIImagePickerControllerDelegate> @property (strong, nonatomic) UIImagePickerController *ipc;/* 相册选择器 */
@property (strong, nonatomic) AVPlayerViewController *playerVC;/* 视频播放器 */
@property (weak, nonatomic) IBOutlet UIImageView *showImageView;/* 显示图片 */ @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//设置显示图片可交互
self.showImageView.userInteractionEnabled = YES;
//创建AVPlayerViewController控制器
AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
playerVC.view.frame = self.showImageView.bounds;
[self.showImageView addSubview:playerVC.view];
self.playerVC = playerVC;
self.playerVC.view.hidden = YES;
}
#pragma mark - UI点击
/* 点击打开本地相册 */
- (IBAction)pickImage:(id)sender
{
//如果正在播放视频,停止播放
if (self.playerVC.player) {
[self.playerVC.player pause];
}
//创建图片选择控制器
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
//设置拾取源类型
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置媒体类型,这里设置图册支持的所有媒体类型,图片和视频
ipc.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:ipc.sourceType];
ipc.delegate = self;//设置代理
ipc.allowsEditing = YES;//设置可编辑
self.ipc = ipc;
//弹出图片选择控制器
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate代理方法
/* 选择了一个图片或者视频后调用 */
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取选择文件的媒体类型
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
NSURL *videoURL = nil;
if ([mediaType isEqualToString:@"public.image"]){//选择了图片
//获取选择的图片
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
//显示图片
self.showImageView.image = selectedImage;
self.showImageView.contentMode = UIViewContentModeScaleAspectFill;
NSLog(@"found an image %@",selectedImage);
//删除视频
self.playerVC.player = nil;
self.playerVC.view.hidden = YES; } else if ([mediaType isEqualToString:@"public.movie"]){//选择了视频
//获取临时保存视频的URL
videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"found a video %@",videoURL);
//直接创建AVPlayer,它内部也是先创建AVPlayerItem,这个只是快捷方法
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
self.playerVC.player = player;
self.playerVC.view.hidden = NO;
}
[self dismissViewControllerAnimated:YES completion:^{
if (videoURL) {
//调用控制器的属性player的开始播放方法
[self.playerVC.player play];
}
}];
}
/* 取消选择后调用 */
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{
//取消选择后继续播放视频
if (self.playerVC.player) {
[self.playerVC.player play];
}
}];
NSLog(@"取消选择");
}
@end

Demo代码点这里:learnDemo里面的PickerTest

获取视频的时候,会有一个拷贝图册里的视频文件到本地沙盒tmp路径下的过程,需要等待拷贝完成,才会调用代理方法。
这一节属于知识补充,就没有很多新内容,大部分内容之前讲过。

最新文章

  1. MySQL - 常用命令及常用查询SQL
  2. Mybatis.Net 整合 ODP.NET Managed
  3. 五大权限:UGO权限、SetUID SetGID Sticky、ACL权限、chattr(文件系统级别的权限)、SELINUX
  4. C#设计模式——迭代器模式(Iterator Pattern)
  5. [TypeScript] TypeScript对象转JSON字符串范例
  6. Win7旗舰版中的IIS配置asp.net的运行环境
  7. 【转】VS2013 C#WinForm程序构造界面拖动控件NumericUpDown时&quot;未响应“是有道词典惹的祸
  8. vm.dirty_background_ratio and vm.dirty_ratio
  9. C# 枚举
  10. ASP.NET MVC5 生成验证码
  11. [ios2] 关于CGBitmapContextCreate【转】
  12. js键盘键值大全
  13. js设计模式--工厂模式
  14. ABB安全区域(全局区域)的指令解析
  15. CSS3 background-size 属性
  16. 小程序之 微信小程序下拉上方出现空白
  17. Petrozavodsk Winter Camp, Day 8, 2014, Second Trip
  18. unity仿微信飞机大战项目
  19. 《大型分布式网站架构》学习笔记--01SOA
  20. codeforces 848B Rooter&#39;s Song 思维题

热门文章

  1. linux python升级及全局环境变量设置
  2. 激光推送报错:APNs is not available,please check your provisioning profile and certification 和 设置别名问题 app not registed, give up set tag:
  3. [学习总结] python语言学习总结 (二)
  4. vue 文件流下载xlsx 功能实现
  5. 树莓派 - 修改pi账号密码,开启root账号
  6. GIT在团队中的最佳实践
  7. iOS开发之蓝牙业务封装
  8. 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!
  9. vue中文本域限制字数的方法
  10. Linux redis服务搭建记录