ios学习笔记——UIImagePickerController
1、UIImagePickerController初始化
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
2、设置属性
1)sourceType属性:数据的来源,有三种来源:
enum {
UIImagePickerControllerSourceTypePhotoLibrary, // 图库
UIImagePickerControllerSourceTypeCamera, // 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相册
};
typedef NSUInteger UIImagePickerControllerSourceType;
2)allowsEditing属性:是否允许剪切
3)设置代理deleage
4) mediaTypes属性:为UIImagePickerViewController指定媒体类型(数组),默认包含kUTTypeImage,所以拍照时可以不用设置,但是录像的时候必须设置,可以设置为kUTTypeVideo(不带声音)或者kUTTypeMovie(视频并带有声音)
设置媒体类型:
1 picker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil];
如果想包含所有媒体,可以使用下面方法:
1 picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
5) videoQuality属性:视频质量,一个枚举类型。
1 enum{2 UIImagePickerControllerQualityTypeHigh = 0, //高清 3 UIImagePickerControllerQualityTypeMedium = 1, //default value 4 UIImagePickerControllerQualityTypeLow = 2, //低质量,适用于蜂窝网传输 5 UIImagePickerControllerQualityType640x480 = 3,6 UIImagePickerControllerQualityTypeIFrame1280x720 = 4,7 UIImagePickerControllerQualityTypeIFrame960x540 = 5 8 };9 typedef NSUInteger UIImagePickerControllerQualityType;
6) videoMaximumDuration属性:录制视频的最长时间,默认长度为10分钟。
7) showsCameraControls属性:是否显示摄像头控制面板,默认为YES。
3、显示UIImagePickerController
[self presentViewController:self.pickerController animated:YES completion:nil];
4、实现代理函数
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
如果实现了imagePickerControllerDidCancel函数,那么我们点击cancel按钮时,失效,所以我们要在这个函数里调用
[picker dismissViewControllerAnimated:YES completion:nil];
简单实现拍照、录像,并从相册中取照片和视频:
SJZPhotoViewController.h
1 #import <UIKit/UIKit.h> 2 3 @protocol SJZPhotoViewControllerDelegate <NSObject> 4 5 @optional6 - (void)getPhotoFromXiangce:(UIImage *)image;7 - (void)getVideoFromXiangce:(NSURL *)strURL;8 @end 9 10 @interfaceSJZPhotoViewController : UIViewController11 12 @property (nonatomic, weak) UIViewController *parentController;13 @property (nonatomic, weak) id<SJZPhotoViewControllerDelegate> delegate;14 @property (nonatomic, assign) CGFloat imageSale;15 16 17 - (void)addPhotoActionSheet;18 - (void)addVideoActionSheet;19 @end
SJZPhotoViewController.m
1 #import "SJZPhotoViewController.h" 2 #import <MobileCoreServices/UTCoreTypes.h> 3 4 @interface SJZPhotoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 5 6 @end 7 8 @implementationSJZPhotoViewController9 10 - (void)viewDidLoad {11 [super viewDidLoad];12 13 14 }15 16 - (void)addVideoActionSheet17 {18 UIAlertController * alert =[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];19 20 UIAlertAction * videoAction = [UIAlertAction actionWithTitle:@"录制视频" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {21 [self actionsheetForVideofromXiangji];22 }];23 [alert addAction:videoAction];24 25 UIAlertAction * action = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {26 [self aciontSheetForVideoFromXiangce];27 }];28 29 [alert addAction:action];30 31 UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {32 33 }];34 [alert addAction:cancleAction];35 36 [self.parentController presentViewController:alert animated:YES completion:^{37 38 }];39 40 }41 42 //从相册取视频 43 - (void)aciontSheetForVideoFromXiangce44 {45 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){46 UIImagePickerController * picker =[[UIImagePickerController alloc] init];47 picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;48 //设置媒体类型 49 picker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];50 picker.delegate =self;51 52 [self.parentController presentViewController:picker animated:YES completion:^{53 54 }];55 }56 }57 58 //录像 59 - (void)actionsheetForVideofromXiangji60 {61 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){62 //判断后置摄像头是否可用 63 if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]){64 UIImagePickerController * picker =[[UIImagePickerController alloc] init];65 picker.sourceType =UIImagePickerControllerSourceTypeCamera;66 picker.delegate =self;67 68 //设置媒体类型,需要包括视频媒体类型 69 picker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil];70 71 [self.parentController presentViewController:picker animated:YES completion:^{72 73 }];74 }75 }76 }77 78 - (void)addPhotoActionSheet79 {80 UIAlertController * alert =[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];81 82 UIAlertAction * action = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {83 [self actionSheetForImageFromXiangji];84 }];85 [alert addAction:action];86 87 UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {88 [self aciontSheetForImageFromXiangce];89 }];90 [alert addAction:action1];91 92 UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {93 94 }];95 [alert addAction:cancelAction];96 97 [self.parentController presentViewController:alert animated:YES completion:nil];98 }99 100 //从相册区图片 101 - (void)aciontSheetForImageFromXiangce102 {103 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){104 UIImagePickerController * picker =[[UIImagePickerController alloc] init];105 picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;106 picker.delegate =self;107 [self.parentController presentViewController:picker animated:YES completion:^{108 }];109 }110 }111 112 //相机拍照 113 - (void)actionSheetForImageFromXiangji114 {115 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){116 UIImagePickerController * pick =[[UIImagePickerController alloc] init];117 pick.sourceType =UIImagePickerControllerSourceTypeCamera;118 pick.delegate =self;119 120 [self.parentController presentViewController:pick animated:YES completion:^{121 }];122 }123 }124 125 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info126 {127 NSString * meidaType =[info objectForKey:UIImagePickerControllerMediaType];128 if([meidaType isEqualToString:@"public.image"]){129 UIImage * image =[info objectForKey:UIImagePickerControllerOriginalImage];130 //在这里可以对图片进行处理(比如压缩) 131 if(self.imageSale > 0){132 NSData * photoData =UIImageJPEGRepresentation(image, self.imageSale);133 image =[UIImage imageWithData:photoData];134 }135 136 if([self.delegaterespondsToSelector:@selector(getPhotoFromXiangce:)]){137 [self.delegategetPhotoFromXiangce:image];138 }139 }else if([meidaType isEqualToString:(NSString *)kUTTypeMovie]){140 //获取视频文件的url 141 NSURL * mediaURL =[info objectForKey:UIImagePickerControllerMediaURL];142 143 if([self.delegaterespondsToSelector:@selector(getVideoFromXiangce:)]){144 [self.delegategetVideoFromXiangce:mediaURL];145 }146 }147 148 [self.parentController dismissViewControllerAnimated:YES completion:nil];149 }150 151 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker152 {153 [self.parentController dismissViewControllerAnimated:YES completion:^{154 155 }];156 }157 158 @end
demo地址:https://github.com/sjzlovecj/SJZPhotoView
转载于:https://www.cnblogs.com/sjzlovecj/p/5561802.html
ios学习笔记——UIImagePickerController相关推荐
- IOS学习笔记39——拍照、从相册选图并对图片进行裁剪
2013第一篇,大家新年快乐!感谢一直关注我博客的同学们,有你们的支持我才有动力越做越好!有阵子没写博客了,因为前阵子着实比较忙,没时间整理,今天主要实现一个小Demo,我们知道在Instagram或 ...
- OpenCV for Ios 学习笔记(4)-标记检测1
本文原始地址:OpenCV for Ios 学习笔记(4)-标记检测1 简单的标记经常是以白色块和黑色块构成的规则图形.因为我们预先知道这些因素,所以我们可以很容易检测标记. 如图: 首先,我们需要找 ...
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法...
IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法 Author:hmjiangqq Email:jiangqqlmj@163.com ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
- iOS学习笔记-自己动手写RESideMenu
代码地址如下: http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars ...
- iOS学习笔记-地图MapKit入门
代码地址如下: http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错 ...
- iOS学习笔记-自定义过渡动画
代码地址如下: http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swi ...
- IOS学习笔记07---C语言函数-scanf函数
2013/8/7 IOS学习笔记07---C语言函数-scanf函数 ------------------------------ qq交流群:创梦技术交流群:251572072 ...
- IOS学习笔记07---C语言函数-printf函数
IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...
最新文章
- java png 转jpg_怎么用java将png图片转换成jpg格式的图片
- table中嵌套table,如何用jquery来控制奇偶行颜色
- 类的方法(通过引用来传递参数)
- OpenCV中 CvArr、Mat、CvMat、IplImage、BYTE转换(总结而来)
- python循环五要素_python常见单词在手,编程入门不愁
- php 类中调用另类,PHP return语句另类用法不止是在函数中,return语句_PHP教程
- win7系统如何访问xp系统的服务器,WIN7系统怎么让XP系统访问呢
- SpringMVC配置没问题却却找不到页面,页面显示404
- 数据结构 - 链表(单向环形链表)(约瑟夫问题)
- H3C 路由度量值(Metric)
- c/c++函数指针(3)
- CUDA编程-02: 初识CUDA编程
- js一键批量打印_前端连接打印机批量打印pdf格式的文件
- 解决xftp6 要继续使用此程序,您必须应用最新的更新或使用新版本
- 数据挖掘模型中的IV和WOE详解
- HTML5浏览器兼容性解决方案
- Java - 类与对象
- 修复被osx86破坏的网卡
- 联邦学习:加密算法Paillier,Affine,IterativeAffine
- 2-13 monthCalendar日历控件
热门文章
- [机器学习-Sklearn]K-means(K均值)学习与总结
- [深度学习]-基于tensorflow的CNN和RNN-LSTM文本情感分析对比
- 用Pytorch实现逻辑回归分类
- Map的value值降序排序与升序排序(java)
- django+mysql+插入数据库网页展示内容
- 数字图像处理--灰度图转伪彩色图
- A. Gamer Hemose
- html给背景架渐变,JS和CSS实现渐变背景特效的代码
- Table阿里云mysql_数据同步-从MySQL到Tablestore-阿里云开发者社区
- ug中模型不见了怎么办_关于UG参数化建模的定义