这里讲的是
使用UIImagePickerController的实现相机相册的调用。需实现
UIImagePickerControllerDelegate,UINavigationControllerDelegate两个协议。
基本实现步骤:
1,初始化UIImagePickerController
2,判断设备是否支持摄像或允许打开相册
3,实现UIImagePickerController协议
4,进行回调
代码:
//
// ViewController.m
// 相机相册OC
//如果想要相册 功能按钮汉化 需在plist文件中修改 Localization native development region value的值为 China
#define Log(log) NSLog(@"Test:%@",log)
#define LLog(c,log) {NSString * str = [[NSString alloc] initWithFormat:@"%@",c]; NSLog(@"%@:%@",str,log);}
#import "ViewController.h"
#import "UIImagePickerController+Judge.h"
#import "AlertViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>//UIImagePickerController需要实现的协议
///获取图片显示
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
///系统提供的用来获取图片和视频的接口,执行UIImagePickerControllerDelegate,UINavigationControllerDelegate 两个协议的回调方法
@property (strong,nonatomic) UIImagePickerController * pickerImage;
//判断是选取相册照片,还是相机拍照
@property BOOL isAlum;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
初始化UIImagePickerController
self.pickerImage =[[UIImagePickerController alloc] init];
}
//打开相机,需使用真机测试,否则会崩溃
- (IBAction)camera:(id)sender {
///代理判断获取资源类型
self.isAlum = NO;
//先设定sourceType
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
///设置数据来源类型,这里设置为Camera
self.pickerImage.sourceType = sourceType;
//图片允许编辑
self.pickerImage.allowsEditing = YES;
///如果设备不支持摄像头 isAvailableCamera 封装好的相机检测,网上有很多,下同
// 这里返回的是 return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if (![_pickerImage isAvailableCamera])
{
//提醒框
[AlertViewController ViewController:self altetViewtitle:@"友情提示" Message:@"设备未检测到摄像头" action:@"确定"];
}else if ([_pickerImage isAvailableCamera] && [_pickerImage isSupportTaklingPhotos])isSupportTaklingPhotos 检测是否可以拍照。
{//如果有摄像头,并且可以拍照
//设置代理
self.pickerImage.delegate = self;
///回调
[self presentViewController:_pickerImage animated:YES completion:nil];
}else
{
///提醒框
[AlertViewController ViewController:self altetViewtitle:@"友情提示" Message:@"请在设置中打开相机权限" action:@"确定"];
}
}
//打开相册
- (IBAction)album:(id)sender {
self.isAlum = YES;
/*
*数据来源有三种:
* 1, UIImagePickerControllerSourceTypePhotoLibrary, ---来自图库
* 2, UIImagePickerControllerSourceTypeCamera, ---来自相机
* 3, UIImagePickerControllerSourceTypeSavedPhotosAlbum ----来自相册
*/
///设置数据来源类型,这里设置为photo library
self.pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
///协议
self.pickerImage.delegate = self;
//图片允许编辑
self.pickerImage.allowsEditing = YES;
/*
*检测相册是否可用,即是否存在数据源
*/
if ([_pickerImage isAvailablePhotoLibrary])
{
///回调
[self presentViewController:_pickerImage animated:YES completion:nil];
}else
{
[AlertViewController ViewController:self altetViewtitle:@"友情提示" Message:@"相册不可用" action:@"确定"];
}
}
#pragma mark-协议
///用户选取完成后调用
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
/* 用户选取的信息都在info里
*info里的键值如下:
* NSString *const UIImagePickerControllerMediaType ;指定用户选择的媒体类型(如下:)
* NSString *const UIImagePickerControllerOriginalImage ;原始图片
* NSString *const UIImagePickerControllerEditedImage ;修改后的图片
* NSString *const UIImagePickerControllerCropRect ;裁剪尺寸
* NSString *const UIImagePickerControllerMediaURL ;媒体的URL
* NSString *const UIImagePickerControllerReferenceURL ;原件的URL
* NSString *const UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效
*/
/*指定用户选择的媒体类型
*UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie
说明:使用指定媒体类型 需包含 <MobileCoreServices/MobileCoreServices.h>库
KUTTypeImage 包含:
const CFStringRef kUTTypeImage ;抽象的图片类型
const CFStringRef kUTTypeJPEG ;
const CFStringRef kUTTypeJPEG2000 ;
const CFStringRef kUTTypeTIFF ;
const CFStringRef kUTTypePICT ;
const CFStringRef kUTTypeGIF ;
const CFStringRef kUTTypePNG ;
const CFStringRef kUTTypeQuickTimeImage ;
const CFStringRef kUTTypeAppleICNS
const CFStringRef kUTTypeBMP;
const CFStringRef kUTTypeICO;
KUTTypeMovie 包含:
const CFStringRef kUTTypeAudiovisualContent ;抽象的声音视频
const CFStringRef kUTTypeMovie ;抽象的媒体格式(声音和视频)
const CFStringRef kUTTypeVideo ;只有视频没有声音
const CFStringRef kUTTypeAudio ;只有声音没有视频
const CFStringRef kUTTypeQuickTimeMovie ;
const CFStringRef kUTTypeMPEG ;
const CFStringRef kUTTypeMPEG4 ;
const CFStringRef kUTTypeMP3 ;
const CFStringRef kUTTypeMPEG4Audio ;
const CFStringRef kUTTypeAppleProtectedMPEG4Audio;
*/
获取数据类型
NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];
///如果是图片类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//如果是图库选择图片,否则是拍照
if (_isAlum) {
//图库选择
[self chosePhoto:picker WithInfo:info];
}else{
///拍照
[self savePhoto:picker WithInfo:info];
}
}else{
}
}
//用户取消选取时调用
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
///拍照存储
-(void)savePhoto:(UIImagePickerController*)picker WithInfo:(NSDictionary<NSString *,id> *)info{
是否保存成功
SEL select = @selector(imageWasSavedSuccessfully: didFinishSavingWithError: contextInfo:);
///选择相片后移除相册
[picker dismissViewControllerAnimated:YES completion:^{
UIImage * image;
//如果允许被编辑,获取编辑后的图片,否则获取原始图片
if (picker.allowsEditing) {
///编辑后的图片
image = [info objectForKey:UIImagePickerControllerEditedImage];
}else{
//原始图片
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
//显示图片
self.imageView.image = image;
/*
*异步保存图片
*/
分组
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
///异步执行
dispatch_async(queue, ^{
dispatch_group_t group = dispatch_group_create();
///异步
dispatch_group_async(group, queue, ^{
/保存到系统相册中·
UIImageWriteToSavedPhotosAlbum(image, self, select, NULL);
});
});
}];
}
///选择照片
-(void)chosePhoto:(UIImagePickerController*)picker WithInfo:(NSDictionary<NSString *,id> *)info{
///选择相片后移除相册
[picker dismissViewControllerAnimated:YES completion:^{
///显示图片
[self getImage:picker Info:info];
}];
}
/显示获取选取后的图片
-(void)getImage:(UIImagePickerController*)picker Info:(NSDictionary<NSString *,id> *)info{
UIImage * image;
//如果允许被编辑,获取编辑后的图片,否则获取原始图片
if (picker.allowsEditing) {
///编辑后的图片
image = [info objectForKey:UIImagePickerControllerEditedImage];
}else{
//原始图片
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
//显示图片
self.imageView.image = image;
}
// 保存图片到相册后,调用的相关方法,查看是否保存成功
- (void)imageWasSavedSuccessfully:(UIImage *)paramImage didFinishSavingWithError:(NSError *)paramError contextInfo:(void *)paramContextInfo{
if (paramError == nil){
Log(@"Image was saved successfully.");
} else {
Log(@"An error happened while saving the image.");
LLog(@"Error = %@", paramError);
}
}
@end
Demo: 点我下载Demo