这篇文章主要说明一下iOS系统相机的基本功能,能使用系统相机进行拍照和保存,或者进入图库、相册选取照片。
授权
iOS10之后,使用相机以及相册功能需要在plist文件中添加权限:
相机权限: Privacy - Camera Usage Description
相册权限: Privacy - Photo Library Usage Description
程序安装后首次使用相机功能系统会弹出一个授权窗口,
屏幕快照 2017-03-07 11.46.49.png
相册也是
屏幕快照 2017-03-07 11.47.00.png这个窗口只弹出一次,要改动的话需要在设置页面进行设置。
如果想要拦截这个窗口的点击事件,需要导入两个框架:
#import <AVFoundation/AVFoundation.h> 相机
#import <Photos/Photos.h>相册
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status) {
case AVAuthorizationStatusNotDetermined:{
//程序是第一次启动,发起授权许可,不管用户接受还是拒绝,只会弹出一次
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
//第一次用户接受
NSLog(@"用户接受");
}else{
//用户拒绝
NSLog(@"用户拒绝");
}
}];
break;
}
case AVAuthorizationStatusAuthorized:{
// 已经开启授权,可继续
NSLog(@"已开启相机权限");
break;
}
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
// 用户已经拒绝过授权,或者相机设备无法访问
NSLog(@"没有权限");
break;
default:
break;
}
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied)
{
// 用户拒绝,跳转到自定义提示页面
NSLog(@"用户拒绝");
}
else if (status == PHAuthorizationStatusAuthorized)
{
// 用户授权,弹出相册对话框
NSLog(@"用户同意");
}
});
}];
使用
UIImagePickerController *_imagePickerController;
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self;
/*
UIImagePickerControllerSourceTypePhotoLibrary, 图库
UIImagePickerControllerSourceTypeCamera, 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum 相册
打开类型,默认为图库
*/
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
/*
UIModalTransitionStyleCoverVertical = 0, 底部滑入
UIModalTransitionStyleFlipHorizontal, 水平翻转
UIModalTransitionStyleCrossDissolve, 交叉溶解
UIModalTransitionStylePartialCurl, 翻页
弹出动画效果
*/
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
_imagePickerController.allowsEditing = YES; //允许编辑,设为NO则直接选中,YES可以调整位置
BOOL camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //是否支持相机功能,其他类型也可判断
NSLog(@"%d",camera);
/*
UIImagePickerControllerQualityTypeHigh = 0, // highest quality
UIImagePickerControllerQualityTypeMedium = 1, // medium quality, suitable for transmission via Wi-Fi
UIImagePickerControllerQualityTypeLow = 2, // lowest quality, suitable for tranmission via cellular network
UIImagePickerControllerQualityType640x480 NS_ENUM_AVAILABLE_IOS(4_0) = 3, // VGA quality
UIImagePickerControllerQualityTypeIFrame1280x720 NS_ENUM_AVAILABLE_IOS(5_0) = 4,
UIImagePickerControllerQualityTypeIFrame960x540 NS_ENUM_AVAILABLE_IOS(5_0) = 5,
拍摄的照片质量,用于不同网络状态下,比如wifi下发送高、中质量的图片,蜂窝数据下发送低质量的图片,默认中等
*/
_imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
/*
UIImagePickerControllerCameraDeviceRear, 后置摄像头
UIImagePickerControllerCameraDeviceFront 前置摄像头
默认后置摄像头,注意:只有在相机类型时才可使用
*/
if (_imagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera) {
_imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
UIImagePickerControllerCameraFlashModeOff = -1, 关闭闪光灯
UIImagePickerControllerCameraFlashModeAuto = 0, 自动
UIImagePickerControllerCameraFlashModeOn = 1 开启
默认自动,注意:只有在相机类型时才可使用
*/
if (_imagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera) {
_imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
}
if (_imagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera) {
_imagePickerController.showsCameraControls = YES; //显示默认UI,NO会隐藏掉标准UI
}
上面只是创建了一个相机对象,需要把它显示出来
[self presentViewController:_imagePickerController animated:YES completion:nil];
协议
使用系统相机需要遵循两个协议
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#pragma mark UIImagePickerControllerDelegate
//保存图片到相册
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString *msg = nil;
if (!error) {
msg = @"图片保存成功";
}
else {
msg = @"图片保存失败";
}
NSLog(@"%@",msg);
}
//点击使用按钮
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
_imgView.image = info[@"UIImagePickerControllerOriginalImage"];//在打开相机后拍的照等于_imageView.image,照到有两个选项,一个重照,一个等于
}
else {
NSLog(@"info = %@",info);
//获取用户编辑之后的图片
_imgView.image = info[@"UIImagePickerControllerEditedImage"];
}
[self dismissViewControllerAnimated:YES completion:nil];
[self saveImageToPhotosAlbum];
}
//点击返回按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(@"返回");
[_imagePickerController dismissViewControllerAnimated:YES completion:nil];
}
保存图片到相册
//保存图片到相册
- (void)saveImageToPhotosAlbum {
//保存图片到相册函数
UIImageWriteToSavedPhotosAlbum(_imgView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
还有如果默认UI下显示的文本为英文,在plist文件中找到Localization native development region,value改成china即可编程中文
网友评论