开篇废话
距离我注册简书已经三四个月了,期间一直没有实现自己的计划。从今天开始,每天写一点东西,工作日大多都是项目中的,周末争取能做个全面的总结或整体知识点之类的。好的,废话说了很多,下面开始今天的内容。
正文
昨天新的项目开始写个人设置部分的内容,当写到修改用户头像的时候,感觉那么一大段根本不变的代码不封装一下实在难受,由此想到了之前在Objccn上看过的一种模式叫行为模式,本想仿照设计一个行为,但是发现和文章中的情形不同的是,文章中是直接传入UIImageView去接收了UIImage,而我这里是要拿到UIImage去执行操作,所以只好加入一个block,下面是我开始实现时候的代码:
CameraPhotoBehavior.h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, BehaviorType) {
BehaviorTypeCamera,
BehaviorTypePhoto
};
@interface CameraPhotoBehavior : NSObject
/**
创建一个操作相机/相册的行为
@param type 区分是相机还是相册
@param targetVC 调用vc
@param finishBlock 获取到图片之后的block
*/
+ (void)behaviorWithType:(BehaviorType)type targetVC:(UIViewController *)targetVC finishBlock:(void(^)(UIImage * image))finishBlock;
@end
CameraPhotoBehavior.m
#import "CameraPhotoBehavior.h"
#import <UIKit/UIKit.h>
@interface CameraPhotoBehavior ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, copy)void (^finishBlock)(UIImage *);
@end
@implementation CameraPhotoBehavior
+ (void)behaviorWithType:(BehaviorType)type targetVC:(UIViewController *)targetVC finishBlock:(void (^)(UIImage * image))finishBlock{
CameraPhotoBehavior *behavior = [[self alloc] init];
//保存block
behavior.finishBlock = finishBlock;
//获取设置过的控件
UIImagePickerController *imagePickView = [behavior imagePickerControllerWithBehaviorType:type];
//展示控件
[targetVC presentViewController:imagePickView animated:YES completion:nil];
}
/**
创建UIImagePickerController控件并根据type进行设置
@param type 相机操作还是相册操作
@return 控件
*/
- (UIImagePickerController *)imagePickerControllerWithBehaviorType:(BehaviorType)type{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
imagePickerController.allowsEditing = YES;
if (type == BehaviorTypePhoto) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}else{
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//录制视频时长,默认10s
imagePickerController.videoMaximumDuration = 15;
//相机类型(拍照、录像...)字符串需要做相应的类型转换
imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
//设置摄像头模式(拍照,录制视频)为录像模式
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
}
return imagePickerController;
}
//结果回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
//判断资源类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
//如果是图片
UIImage *image = info[UIImagePickerControllerEditedImage];
self.finishBlock(image);
}
}
@end
OK,完成了,那下来就是使用了,但是使用的时候能成功打开相机,但是死活拿不到图片,控制台打印如下log:
[Generic] Creating an image format with an unknown type is an error
没碰见过这问题,果断开始google,然后在Stackoverflow上看到有人说是没有设置代理,我回头看我的代码是设置的有代理:
imagePickerController.delegate = self;
不是这个原因,那么是什么原因呢?
后来猜想是不是因为实例对象被释放了?然后就回去看代码
+ (void)behaviorWithType:(BehaviorType)type targetVC:(UIViewController *)targetVC finishBlock:(void (^)(UIImage * image))finishBlock{
CameraPhotoBehavior *behavior = [[self alloc] init];
//保存block
behavior.finishBlock = finishBlock;
//获取设置过的控件
UIImagePickerController *imagePickView = [behavior imagePickerControllerWithBehaviorType:type];
//展示控件
[targetVC presentViewController:imagePickView animated:YES completion:nil];
}
上面是第一个方法,我在内部创建了一个实例变量,然后保存......等等!!方法内部创建的对象有效范围只是在方法内部,出了方法就会被释放掉的。好吧,看来是我忽视了变量作用域的结果,最后修改如下:
//用一个全局变量来保存实例对象
CameraPhotoBehavior *behavior;
+ (void)behaviorWithType:(BehaviorType)type targetVC:(UIViewController *)targetVC finishBlock:(void (^)(UIImage * image))finishBlock{
behavior = [[self alloc] init];
//保存block
behavior.finishBlock = finishBlock;
//获取设置过的控件
UIImagePickerController *imagePickView = [behavior imagePickerControllerWithBehaviorType:type];
//展示控件
[targetVC presentViewController:imagePickView animated:YES completion:nil];
}
然后问题解决了!
总结
这次能解决还算是比较幸运,没有耗费特别多的时间。其实回头来看这个问他就是一个很小很小的问他,但是在平时的工作中我都经常忽视掉了作用域的问题,这给了我警醒:以后遇到类似的问题可以从这个方面进行一些思考。
OVER