iOS照片与相册1——UIImagePickerControll
前言
大多数APP都有访问相机和相册功能,所以有必要研究一下。这段时间准备集中精力研究研究iOS中访问相机和相册这块。大概会分三篇,分别会介绍UIImagePickerController
,AssetsLibrary
和PhotoKit
,包括其API解释,并尝试封装。
API解释
UIImagePickerController
里封装了访问相机和相册的整个流程:打开相机拍照,然后编辑照片,确认是否选取;打开相册,选取图片,编辑图片等步骤。我们只需要在创建其实例对象后对暴露的属性进行一些设置,以满足我们的需求。
对于其属性和方法的用途,我们可以进入UIImagePickerController.h
观察,另外,下面两篇博文对其属性和方法的用途解释的比较全面,可以查看:
《UIImagePickerController从拍照、图库、相册获取图片》
《UIImagePickerController简单使用》
封装
为了使项目中访问相机和相册更简洁方便,我们可以基于UIImagePickerController
进行封装。封装于一个功能类PhotoPickManager
。
** PhotoPickManager.h **
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, PickerType)
{
PickerType_Camera = 0, // 拍照
PickerType_Photo, // 照片
};
typedef void(^CallBackBlock)(NSDictionary *infoDict, BOOL isCancel); // 回调
@interface PhotoPickManager : NSObject
+ (instancetype)shareInstance; // 单例
- (void)presentPicker:(PickerType)pickerType target:(UIViewController *)vc callBackBlock:(CallBackBlock)callBackBlock;
@end
** PhotoPickManager.m **
#import "PhotoPickManager.h"
@interface PhotoPickManager ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImagePickerController *_imgPickC;
UIViewController *_vc;
CallBackBlock _callBackBlock;
}
@end
@implementation PhotoPickManager
+ (instancetype)shareInstance
{
static dispatch_once_t once;
static PhotoPickManager *pickManager;
dispatch_once(&once, ^{
pickManager = [[PhotoPickManager alloc] init];
});
return pickManager;
}
- (instancetype)init
{
if([super init]){
if(!_imgPickC){
_imgPickC = [[UIImagePickerController alloc] init]; // 初始化 _imgPickC
}
}
return self;
}
- (void)presentPicker:(PickerType)pickerType target:(UIViewController *)vc callBackBlock:(CallBackBlock)callBackBlock
{
_vc = vc;
_callBackBlock = callBackBlock;
if(pickerType == PickerType_Camera){
// 拍照
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
_imgPickC.delegate = self;
_imgPickC.sourceType = UIImagePickerControllerSourceTypeCamera;
_imgPickC.allowsEditing = YES;
_imgPickC.showsCameraControls = YES;
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor grayColor];
_imgPickC.cameraOverlayView = view;
[_vc presentViewController:_imgPickC animated:YES completion:nil];
}else{
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"" message:@"相机不可用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
[alertV show];
}
}
else if(pickerType == PickerType_Photo){
// 相册
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
_imgPickC.delegate = self;
_imgPickC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
_imgPickC.allowsEditing = YES;
[_vc presentViewController:_imgPickC animated:YES completion:nil];
}else{
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"" message:@"相册不可用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
[alertV show];
}
}
}
#pragma mark ---- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[_vc dismissViewControllerAnimated:YES completion:^{
_callBackBlock(info, NO); // block回调
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[_vc dismissViewControllerAnimated:YES completion:^{
_callBackBlock(nil, YES); // block回调
}];
}
//- (void)dealloc
//{
// _imgPickC.delegate = nil;
//}
@end
调用部分代码:
** HomeViewController.m **
#import "HomeViewController.h"
#import "PhotoPickManager.h"
@interface HomeViewController ()<UIActionSheetDelegate>
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"选取照片";
}
- (IBAction)pickBtnClick:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
PhotoPickManager *pickManager = [PhotoPickManager shareInstance];
[pickManager presentPicker:buttonIndex
target:self
callBackBlock:^(NSDictionary *infoDict, BOOL isCancel) {
_imgView.image = [infoDict valueForKey:UIImagePickerControllerOriginalImage];
}];
}
@end
运行后最终效果:
PhotoPicker.gif一个Bug的排除过程
一开始写的时候,并没有把PhotoPickManager
写成创建单例对象,程序运行崩溃,是野指针错误,即某对象已经释放,但你却对其进行了操作。内存崩溃错误比较难追踪,XCode的调试日志里也没什么信息,所以我在网上百度到了通过僵尸模式定位bug的东西,即:NSZombieEnabled
。同时摁command+option+R,会弹出窗口选择Arguments,然后在Environment Variables一栏中添加NSZombieEnabled为YES的属性。
当设置NSZombieEnabled环境变量后,一个对象销毁时会被转化为_NSZombie,设置NSZombieEnabled后,当你向一个已经释放的对象发送消息,这个对象就不会向之前那样Crash或者产生一个难以理解的行为,而是放出一个错误消息,然后以一种可预测的可以产生debug断点的方式消失, 因此我们就可以找到具体或者大概是哪个对象被错误的释放了。
设置该环境变量后,同样的问题运行至崩溃时便有了日志提示。
屏幕快照 2016-07-06 下午3.37.18.png
好像也并没有很有用,然后我就复制粘贴bug日志信息到百度了,找到了一篇博文是这么说的。
屏幕快照 2016-07-06 下午3.39.38.png然后我便在PhotoPickManager.m
中添加了dealloc代码。
- (void)dealloc
{
_imgPickC.delegate = nil;
}
然后,再次Run......程序没有崩溃!但是......
但是当选取了照片后我发现代码不走UIImagePickerController
的代理方法了,这就是因为上步在dealloc中把_imgPickC的代理置为nil导致的。UIImagePickerController
封装的代码里正在进行照片的选取,选取后会触发代理方法,但是此时你却将其代理置为了nil,所以当然不会走代理方法。
为了避免选取照片的步骤正在进行中,但PhotoPickManager
被释放的尴尬场景,我便将其改为了单例,这样创建的实例是static类型,一直存在的。
结语
通过UIImagePickerController
能实现简单而基本的访问相机和相册功能,但若想实现照片多选,选取页面自定制,照片分类等个性化功能,还需了解AssetsLibrary
和PhotoKit
的知识,并自己进行封装。