IOS打开照相机与本地相册选择图片实例详解
程序员文章站
2023-12-19 13:28:16
ios打开照相机与本地相册选择图片
最近正好项目里面要集成“打开照相机与本地相册选择图片”的功能,今天就在这边给大家写一个演示程序;打开相机拍摄后或者在相册中选择一张照片...
ios打开照相机与本地相册选择图片
最近正好项目里面要集成“打开照相机与本地相册选择图片”的功能,今天就在这边给大家写一个演示程序;打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上。好了废话不多说,因为比较简单直接上源码。
首先,我们在头文件中添加需要用到的actionsheet控件,显示图片的uiimageview控件,并且加上所需要的协议
#import <uikit/uikit.h> @interface imagepickerviewcontroller : uiviewcontroller<uiimagepickercontrollerdelegate,uiactionsheetdelegate,uinavigationcontrollerdelegate> @property (strong, nonatomic) iboutlet uiimageview *headimage; @property (strong, nonatomic) uiactionsheet *actionsheet; - (ibaction)clickpickimage:(id)sender; @end
通过点击我设置在界面中的按钮来呼出actionsheet控件,来选择相应的操作拍照或是在相册中选择相片,代码如下:
// // imagepickerviewcontroller.m // testauto // // created by silicon on 15/5/9. // copyright (c) 2015年 silicon. all rights reserved. // #import "imagepickerviewcontroller.h" @interface imagepickerviewcontroller () @end @implementation imagepickerviewcontroller @synthesize actionsheet = _actionsheet; - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view from its nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } /** @ 调用actionsheet */ - (void)callactionsheetfunc{ if([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera]){ self.actionsheet = [[uiactionsheet alloc] initwithtitle:@"选择图像" delegate:self cancelbuttontitle:@"取消" destructivebuttontitle:nil otherbuttontitles:@"拍照", @"从相册选择", nil nil]; }else{ self.actionsheet = [[uiactionsheet alloc] initwithtitle:@"选择图像" delegate:self cancelbuttontitle:@"取消"destructivebuttontitle:nil otherbuttontitles:@"从相册选择", nil nil]; } self.actionsheet.tag = 1000; [self.actionsheet showinview:self.view]; } // called when a button is clicked. the view will be automatically dismissed after this call returns - (void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex{ if (actionsheet.tag == 1000) { nsuinteger sourcetype = uiimagepickercontrollersourcetypephotolibrary; // 判断是否支持相机 if([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) { switch (buttonindex) { case 0: //来源:相机 sourcetype = uiimagepickercontrollersourcetypecamera; break; case 1: //来源:相册 sourcetype = uiimagepickercontrollersourcetypephotolibrary; break; case 2: return; } } else { if (buttonindex == 2) { return; } else { sourcetype = uiimagepickercontrollersourcetypesavedphotosalbum; } } // 跳转到相机或相册页面 uiimagepickercontroller *imagepickercontroller = [[uiimagepickercontroller alloc] init]; imagepickercontroller.delegate = self; imagepickercontroller.allowsediting = yes; imagepickercontroller.sourcetype = sourcetype; [self presentviewcontroller:imagepickercontroller animated:yes completion:^{ }]; } } - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { [picker dismissviewcontrolleranimated:yes completion:^{ }]; uiimage *image = [info objectforkey:uiimagepickercontrolleroriginalimage]; self.headimage.image = image; } /* #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. } */ - (ibaction)clickpickimage:(id)sender { [self callactionsheetfunc]; } @end
代码比较简单,也容易理解,运行的效果如下:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!