在实际的APP开发中,我们经常会见到应用的这样的功能 :需要选取手机相册的照片,还有选取视频,拍视频和照相的操作.

在iOS开发中,实现以上的功能就需要用到 UIImagePickerController.

现将 UIImagePickerController的基本用法总结如下 :

 #import "ViewController.h"

 @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

 //拍照
- (IBAction)takePhoto:(id)sender; //拍电影
- (IBAction)takeMovie:(id)sender; @property (weak, nonatomic) IBOutlet UIImageView *imgView; //选择照片
- (IBAction)selectPhoto:(id)sender; //选择视频
- (IBAction)selectVideo:(id)sender; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; //UIImagePickerController.访问用户相册 2.拍照,拍视频 } //1.选取照片
- (IBAction)selectPhoto:(id)sender { UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; /*
UIImagePickerControllerSourceTypePhotoLibrary,获取相册中所有的文件
UIImagePickerControllerSourceTypeCamera,摄像头
UIImagePickerControllerSourceTypeSavedPhotosAlbum,系统内置相册
*/ //资源类型(资源来自哪里,可以来相册,摄像头)
imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //设置代理
imagePickerCtrl.delegate = self; //弹出控制器
[self presentViewController:imagePickerCtrl animated:YES completion:^{ //弹出控制器完成调用的方法
}]; }
/**
*
* 2.选取视频
*/
- (IBAction)selectVideo:(id)sender { UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePickerCtrl.delegate = self; //指定媒体类型,图片(@"public.image"),视频(@"public.movie")
imagePickerCtrl.mediaTypes = @[/*@"public.image",*/@"public.movie"]; [self presentViewController:imagePickerCtrl animated:YES completion:NULL]; } //3.拍照
- (IBAction)takePhoto:(id)sender { UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; imagePickerCtrl.delegate = self; //判断手机是否支持(前)摄像头
if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) { UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机太渣,没有摄像头" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alerView show]; return;
} //指定资源来自摄像头
imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePickerCtrl animated:YES completion:NULL]; } //4.拍视频
- (IBAction)takeMovie:(id)sender { UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; imagePickerCtrl.delegate = self; //指定资源来自摄像头
imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera; //设置资源类型
imagePickerCtrl.mediaTypes = @[@"public.movie"]; [self presentViewController:imagePickerCtrl animated:YES completion:NULL];
} #pragma mark UIImagePickerControllerDelegate
//选取完视频,或者照片调用的协议方法(不管是拍照,选相册,拍视频,选视频,最终都会调用此方法)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"%@",info); //取出选择的照片
UIImage *img = info[UIImagePickerControllerOriginalImage]; //将选取的照片交给_imgView显示
_imgView.image = img; //如果资源来自摄像头
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { //将图片存到相册中调用的方法 (苹果建议的写法:好像必须要这样写)
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} //如果选中的是视频则可以通过UIImagePickerControllerMediaURL在字典中获取到选中的视频的URL //选取照片或视频后,关闭控制器
[picker dismissViewControllerAnimated:YES completion:NULL]; } //"取消"按钮被点击
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { NSLog(@"被关闭了");
[picker dismissViewControllerAnimated:YES completion:NULL]; } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ NSLog(@"照片保存到相册成功");
} @end

这种写法的缺点就是,选取图片的时候只能选取一张照片,如果要选取多张照片的话,就需要用到另外一种方法  AssetsLibrary

AssetsLibrary的基本用法随后更新!!!    记得关注我哟!!!!

UIImagePickerController的用法的更多相关文章

  1. 给iOS开发新手送点福利,简述UIImagePickerController的属性和用法

    1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;         // 检查指定源是否在设备上 ...

  2. ios开发 <AppName>-Prefix.pch文件的用法详解

    我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...

  3. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  4. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  5. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  6. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  7. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  8. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  9. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

随机推荐

  1. 解决在jenkins中无法打开robot framework report.html log.html的问题

    问题描述: Opening Robot Framework report failed Verify that you have JavaScript enabled in your browser. ...

  2. POJ1185 炮兵阵地 和 POJ2411 Mondriaan's Dream

    炮兵阵地 Language:Default 炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34008 Accepted ...

  3. 用vue开发一个所谓的数独

    1.前言 最近的后台管理系统页面,功能暂时没有新的需求,就在想首页放什么东西,最近我想到的就是放个所谓的数独,为什么是所谓的数独,因为规则不同于标准的数独,只要求每一行每一列数字不一样就可以了!这个实 ...

  4. Spring Boot学习--spring-boot-starter-parent及starters(转)

    在官方文档的第三部分的13块讲述了引用的管理,官方推荐的是使用Maven和Gradle. 我一直在用的是maven,而且使用maven有些优势–spring-boot-starter-parent,这 ...

  5. luogu 1712

    按区间长度降序排序维护区间指针 [l, r],第 l ~ r 条线段 表示当前区间可以满足条件那么 r 后移一定不是更优的因此 l 前移,使得 r 后移过程中取最小值更新 answer #includ ...

  6. P1928 外星密码

    题目描述 有了防护伞,并不能完全避免 2012 的灾难.地球防卫小队决定去求助外星种族的帮 助.经过很长时间的努力,小队终于收到了外星生命的回信.但是外星人发过来的却是一 串密码.只有解开密码,才能知 ...

  7. P2734 游戏 A Game

    题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的得分中 ...

  8. 使用python画一颗圣诞树

    # -*- coding: utf-8 -*- # @Time : 18-12-26 上午9:32 # @Author : Felix Wang import turtle # 定义圣诞树的绿叶函数 ...

  9. Ideal打war包和tomcat展示War包

    今天主要是介绍如何把java代码把成war包以及如何在tomcat中放置展示.比较简单.大家可以看看 刚开始ideal 不知道打包,网上搜索了一个教程,看了半天没看会. 主要还是说的不太明白 . 不过 ...

  10. 【零基础】神经网络优化之L1、L2

    一.序言 前面的文章中,我们逐步从单神经元.浅层网络到深层网络,并且大概搞懂了“向前传播”和“反向传播”的原理,比较而言深层网络做“手写数字”识别已经游刃有余了,但神经网络还存在很多问题,比如最常见的 ...