iOS开发-调用系统相机和相册获取照片示例
前言:相信大家都知道大部分的app都是有我的模块的,而在我的模块基本都有用户的头像等信息,并且是可以更改头像的。那么今天小编给大家简单介绍一下iOS开发中如何调用系统相机拍照或者相册获取照片。要获取系统相机或者相册,我们需要使用到UIImagePickerController这个类。下面我们来看一下如何实现:
首先,需要遵循UIImagePickerController代理的两个协议:<UIImagePickerControllerDelegate,UINavigationControllerDelegate>。为什么是两个协议呢?你按着command键,点击UIImagePickerController的delegate就会发现其实这个代理遵循了两个协议。
#import"HeaderPhotoViewController.h"
@interfaceHeaderPhotoViewController()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UIImageView*imageView;
@end
@implementationHeaderPhotoViewController
-(void)viewDidLoad{
[superviewDidLoad];
self.navigationItem.title=@"设置头像";
self.view.backgroundColor=[UIColorwhiteColor];
[selfsetNavigation];
[selfaddSubviews];
[selfmakeConstraintsForUI];
}
#pragmamark-setnavigation
-(void)setNavigation{
self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCameratarget:selfaction:@selector(selectPhoto:)];
}
#pragmamark-navitationitemaction
-(void)selectPhoto:(UIBarButtonItem*)itemCamera{
//创建UIImagePickerController对象,并设置代理和可编辑
UIImagePickerController*imagePicker=[[UIImagePickerControlleralloc]init];
imagePicker.editing=YES;
imagePicker.delegate=self;
imagePicker.allowsEditing=YES;
//创建sheet提示框,提示选择相机还是相册
UIAlertController*alert=[UIAlertControlleralertControllerWithTitle:@"请选择打开方式"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];
//相机选项
UIAlertAction*camera=[UIAlertActionactionWithTitle:@"相机"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction){
//选择相机时,设置UIImagePickerController对象相关属性
imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
imagePicker.modalPresentationStyle=UIModalPresentationFullScreen;
imagePicker.mediaTypes=@[(NSString*)kUTTypeImage];
imagePicker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModePhoto;
//跳转到UIImagePickerController控制器弹出相机
[selfpresentViewController:imagePickeranimated:YEScompletion:nil];
}];
//相册选项
UIAlertAction*photo=[UIAlertActionactionWithTitle:@"相册"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction){
//选择相册时,设置UIImagePickerController对象相关属性
imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
//跳转到UIImagePickerController控制器弹出相册
[selfpresentViewController:imagePickeranimated:YEScompletion:nil];
}];
//取消按钮
UIAlertAction*cancel=[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction){
[selfdismissViewControllerAnimated:YEScompletion:nil];
}];
//添加各个按钮事件
[alertaddAction:camera];
[alertaddAction:photo];
[alertaddAction:cancel];
//弹出sheet提示框
[selfpresentViewController:alertanimated:YEScompletion:nil];
}
#pragmamark-addsubviews
-(void)addSubviews{
[self.viewaddSubview:self.imageView];
}
#pragmamark-makeconstraints
-(void)makeConstraintsForUI{
__weaktypeof(self)weakSelf=self;
[_imageViewmas_makeConstraints:^(MASConstraintMaker*make){
make.size.mas_equalTo(CGSizeMake(Screen_Width,Screen_Width));
make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
make.centerY.mas_equalTo(weakSelf.view.mas_centerY);
}];
}
#pragmamark-imagePickerControllerdelegate
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary<NSString*,id>*)info{
[pickerdismissViewControllerAnimated:YEScompletion:nil];
//获取到的图片
UIImage*image=[infovalueForKey:UIImagePickerControllerEditedImage];
_imageView.image=image;
}
#pragmamark-setterandgetter
-(UIImageView*)imageView{
if(!_imageView){
_imageView=[[UIImageViewalloc]init];
_imageView.backgroundColor=[UIColorgreenColor];
_imageView.contentMode=UIViewContentModeScaleAspectFill;
}
return_imageView;
}
@end
OK!demo的所有代码都已经给大家呈现出来了,最后一步就是配置plist文件,千万不要忘了这个,要不会崩的。plist文件里边添加调用相机的字段Privacy-CameraUsageDescription和调用相册的字段:Privacy-PhotoLibraryUsageDescription。万事俱备,就差一个测试的苹果手机了,相机的测试需要使用真机测试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。