[iOS 相机相册调用] UIImagePickerController 简单实用 [转]
程序员文章站
2022-04-13 17:41:44
在ios中要拍照和录制视频最简单的方式就是调用uiimagepickercontroller,uiimagepickercontroller继承与uinavigationcontroller,需要使...
在ios中要拍照和录制视频最简单的方式就是调用uiimagepickercontroller,uiimagepickercontroller继承与uinavigationcontroller,需要使用代理方法时需要同时遵守这两个协议,以前可能比较多的是使用uiimagepickercontroller来选择相册图片或者拍摄图片,其实它的功能还能用来拍摄视频。
使用uiimagepickercontroller拍照或者拍视频主要以下几个步骤: 创建一个全局的uiimagepickercontroller对象。 指定uiimagepickercontroller的来源sourcetype,是来自uiimagepickercontrollersourcetypecamera相机,还是来自uiimagepickercontrollersourcetypephotolibrary相册。
然后是设置mediatypes媒体类型,这是录制视频必须设置的选项,默认情况下是kuttypeimage(注意:mediatypes的设置是在mobilecoreservices框架下),同还可以设置一些其他视频相关的属性,例如:videoquality视频的质量、videomaximumduration视频的最大录制时长(默认为10s),cameradevice摄像头的方向(默认为后置相机)。
指定相机的捕获模式cameracapturemode,设置mediatypes后在设置捕获模式,注意的是捕获模式需要在相机来源sourcetype为相机时设置,否则会出现crash。 适时的展示uiimagepickercontroller,然后在相应的代理方法保存和获取图片或视频。
下面还是上代码吧,更加清晰明了…
首先需要导入以下用到的几个头文件,同时遵守两个代理方法
#import "viewcontroller.h" #import #import #import @interface viewcontroller () { uiimagepickercontroller *_imagepickercontroller; }
创建uiimagepickercontroller对象
- (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view, typically from a nib _imagepickercontroller = [[uiimagepickercontroller alloc] init]; _imagepickercontroller.delegate = self; _imagepickercontroller.modaltransitionstyle = uimodaltransitionstylefliphorizontal; _imagepickercontroller.allowsediting = yes;
从摄像头获取图片或视频
#pragma mark 从摄像头获取图片或视频 - (void)selectimagefromcamera { _imagepickercontroller.sourcetype = uiimagepickercontrollersourcetypecamera; //录制视频时长,默认10s _imagepickercontroller.videomaximumduration = 15; //相机类型(拍照、录像...)字符串需要做相应的类型转换 _imagepickercontroller.mediatypes = @[(nsstring *)kuttypemovie,(nsstring *)kuttypeimage]; //视频上传质量 //uiimagepickercontrollerqualitytypehigh高清 //uiimagepickercontrollerqualitytypemedium中等质量 //uiimagepickercontrollerqualitytypelow低质量 //uiimagepickercontrollerqualitytype640x480 _imagepickercontroller.videoquality = uiimagepickercontrollerqualitytypehigh; //设置摄像头模式(拍照,录制视频)为录像模式 _imagepickercontroller.cameracapturemode = uiimagepickercontrollercameracapturemodevideo; [self presentviewcontroller:_imagepickercontroller animated:yes completion:nil]; }
从相册获取图片或视频
#pragma mark 从相册获取图片或视频 - (void)selectimagefromalbum { //nslog(@"相册"); _imagepickercontroller.sourcetype = uiimagepickercontrollersourcetypephotolibrary; [self presentviewcontroller:_imagepickercontroller animated:yes completion:nil]; }
在imagepickercontroller:didfinishpickingmediawithinfo:代理方法中处理得到的资源,保存本地并上传…
#pragma mark uiimagepickercontrollerdelegate //该代理方法仅适用于只选取图片时 - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingimage:(uiimage *)image editinginfo:(nullable nsdictionary *)editinginfo { nslog(@"选择完毕----image:%@-----info:%@",image,editinginfo); }
//适用获取所有媒体资源,只需判断资源类型 - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info{ nsstring *mediatype=[info objectforkey:uiimagepickercontrollermediatype]; //判断资源类型 if ([mediatype isequaltostring:(nsstring *)kuttypeimage]){ //如果是图片 self.imageview.image = info[uiimagepickercontrollereditedimage]; //压缩图片 nsdata *filedata = uiimagejpegrepresentation(self.imageview.image, 1.0); //保存图片至相册 uiimagewritetosavedphotosalbum(self.imageview.image, self, @selector(image:didfinishsavingwitherror:contextinfo:), null); //上传图片 [self uploadimagewithdata:filedata]; }else{ //如果是视频 nsurl *url = info[uiimagepickercontrollermediaurl]; //播放视频 _movieplayer.contenturl = url; [_movieplayer play]; //保存视频至相册(异步线程) nsstring *urlstr = [url path]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ if (uivideoatpathiscompatiblewithsavedphotosalbum(urlstr)) { uisavevideoatpathtosavedphotosalbum(urlstr, self, @selector(video:didfinishsavingwitherror:contextinfo:), nil); } }); nsdata *videodata = [nsdata datawithcontentsofurl:url]; //视频上传 [self uploadvideowithdata:videodata]; } [self dismissviewcontrolleranimated:yes completion:nil]; }
图片和视频保存完毕后的回调
#pragma mark 图片保存完毕的回调 - (void) image: (uiimage *) image didfinishsavingwitherror:(nserror *) error contextinfo: (void *)contextinf{ } #pragma mark 视频保存完毕的回调 - (void)video:(nsstring *)videopath didfinishsavingwitherror:(nserror *)error contextinfo:(void *)contextinf{ if (error) { nslog(@"保存视频过程中发生错误,错误信息:%@",error.localizeddescription); }else{ nslog(@"视频保存成功."); } }
以上仅是简单功能的实现,还有例如切换前后摄像头、闪光灯设置、对焦、曝光模式等更多功能…
以下为自己的应用 仅供自己翻阅
// // complaintviewcontroller.m // quickpos // // created by lff on 16/8/1. // copyright ? 2016年 张倡榕. all rights reserved. // #import "complaintviewcontroller.h" #import #import #import @interface complaintviewcontroller () { nsmutablearray *_dataarray; request *_rep; uiimagepickercontroller *_imagepickercontroller; } @property (weak, nonatomic) iboutlet uiview *bgview1;//模块1 @property (weak, nonatomic) iboutlet uiview *bgview2; @property (weak, nonatomic) iboutlet uiview *bgview3; @property (weak, nonatomic) iboutlet uitextfield *nameproduct; @property (weak, nonatomic) iboutlet uitextfield *sizeproduct; @property (weak, nonatomic) iboutlet uitextfield *dateproduct; @property (weak, nonatomic) iboutlet uitextfield *username; @property (weak, nonatomic) iboutlet uitextfield *mobile; @property (weak, nonatomic) iboutlet uitextfield *email; @property (weak, nonatomic) iboutlet uitextfield *address; @property (weak, nonatomic) iboutlet uilabel *photolab; @property (weak, nonatomic) iboutlet uiimageview *photoimageshoew; @end @implementation complaintviewcontroller - (void)viewdidappear:(bool)animated{ [super viewdidappear:animated]; self.navigationcontroller.navigationbar.x = 0; } - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view from its nib. self.title = @"投诉建议"; self.view.backgroundcolor = [common hexstringtocolor:@"eeeeee"]; _rep = [[request alloc] initwithdelegate:self]; [self setimagepickercontroller]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } - (void)setimagepickercontroller{ _imagepickercontroller = [[uiimagepickercontroller alloc] init]; _imagepickercontroller.delegate = self; _imagepickercontroller.allowsediting = yes; _imagepickercontroller.modaltransitionstyle = uimodaltransitionstylefliphorizontal; } //相机/相册 - (ibaction)cameragetbtn:(id)sender { [common pstaalertwithtitle:@"提示" message:@"请选择图片来源方式" defaulttitle:@"相册" cancletitle:@"相机" defaultblock:^(id defaultblock) { _imagepickercontroller.sourcetype = uiimagepickercontrollersourcetypephotolibrary; [self presentviewcontroller:_imagepickercontroller animated:yes completion:nil]; } cancleblock:^(id cancleblock) { _imagepickercontroller.sourcetype = uiimagepickercontrollersourcetypecamera; _imagepickercontroller.videomaximumduration = 10; _imagepickercontroller.mediatypes = @[(nsstring*)kuttypemovie,(nsstring*)kuttypeimage]; _imagepickercontroller.videoquality = uiimagepickercontrollerqualitytypelow; _imagepickercontroller.cameracapturemode = uiimagepickercontrollercameracapturemodephoto; [self presentviewcontroller:_imagepickercontroller animated:yes completion:nil]; } ctr:self]; } #pragma mark - imagepickercontrollerdelegate //适用获取所有媒体资源,只需判断资源类型 -(void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info{ nsstring *mdeiatye = [info objectforkey:uiimagepickercontrollermediatype]; //获取媒体类型 if ([mdeiatye isequaltostring:(nsstring*)kuttypeimage]) { //判断媒体类型是图片 uiimage *image = info[uiimagepickercontrollereditedimage]; nsdata *data = uiimagejpegrepresentation(image, 1.0); nsstring *iamgestrbase64 = [data base64encoding]; nslog(@"%@,%lu",iamgestrbase64,iamgestrbase64.length/1024); _photolab.text = @"照片已选择"; _photoimageshoew.image = image; } [self dismissviewcontrolleranimated:yes completion:nil]; } //提交 - (ibaction)updatebtn:(id)sender { [_rep postcomplaintwithtagid:@"f1060f560c2002e0" tagsn:@"000000000777" tagsnproducercode:@"shfda" enterpriseid:@"3" productid:@"47" username:@"inesa芥花油" mobile:@"15151474388" comments:@"123"]; [common pstaalertwithtitle:@"" message:@"已提交" defaulttitle:@"留在本页" cancletitle:@"返回主页" defaultblock:^(id defaultblock) { nslog(@"123"); } cancleblock:^(id cancleblock) { [self.navigationcontroller poptorootviewcontrolleranimated:yes]; } ctr:self]; } -(void)responsewithdict:(nsdictionary *)dict requesttype:(nsinteger)type{ if (type == requset_complaint) { } } - (void)viewdiddisappear:(bool)animated{ [super viewdiddisappear:animated]; // _photoimageshoew.ima } /* #pragma mark - navigation // in a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // get the new view controller using [segue destinationviewcontroller]. // pass the selected object to the new view controller. } */ @end
上一篇: jquery.onoff代码实现简单的开关按钮功能
下一篇: 并发编程(一)------同步类容器