在使用UIImagePickerController时,常常需要写代理方法实现,感觉麻烦,自己封装了一个简单的方法,通过代码块来实现回调,以避免冗余。
相关代码如下:
1、.h文件
#import <UIKit/UIKit.h>
@interface ImagePickerManager : UIImagePickerController
///设置代码块属性-成功
@property (nonatomic, copy) void (^succeedBack)(UIImage *image);
///设置代码块属性-失败
@property (nonatomic, copy) void (^errorBack) (void);
///设置属性源
@property (nonatomic, assign) UIImagePickerControllerSourceType pickerType;
///根据数据源异常处理
- (BOOL)exceptionHandlingwithSourceType;
///设置代码块回调函数
- (void)getPickerImage:(void (^)(UIImage *image))succeed withError:(void (^)(void))error;
@end
2、.m文件
#import "ImagePickerManager.h"
@interface ImagePickerManager () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@end
@implementation ImagePickerManager
@synthesize succeedBack;
@synthesize errorBack;
@synthesize pickerType;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.sourceType = self.pickerType;
self.delegate = self;
self.allowsEditing = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 异常处理
//根据数据源异常处理
- (BOOL)exceptionHandlingwithSourceType
{
if (![UIImagePickerController isSourceTypeAvailable:self.pickerType])
{
NSString *message = (self.pickerType == UIImagePickerControllerSourceTypeCamera ? @"该设备找不到相机" : @"资源不可访问");
[[[UIAlertView alloc] initWithTitle:@"提示"
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil] show];
return NO;
}
else
{
return YES;
}
}
#pragma mark - 代码块回调
- (void)getPickerImage:(void (^)(UIImage *image))succeed withError:(void (^)(void))error
{
self.succeedBack = [succeed copy];
self.errorBack = [error copy];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
if (!image)
{
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
if (self.succeedBack)
{
self.succeedBack(image);
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
if (self.errorBack)
{
self.errorBack();
}
}
3、使用方法
步骤1导入头文件
#import "ImagePickerManager.h"
步骤2定义属性
@property (nonatomic, strong) ImagePickerManager *imagePickerManager;
步骤3实例化
mySelf.imagePickerManager = [[ImagePickerManager alloc] init];
步骤4设置数据源
mySelf.imagePickerManager.pickerType = UIImagePickerControllerSourceTypePhotoLibrary;
步骤5异常判断
if ([mySelf.imagePickerManager exceptionHandlingwithSourceType])
{
[mySelf presentViewController:mySelf.imagePickerManager animated:YES completion:NULL];
[mySelf.imagePickerManager getPickerImage:^(UIImage *image) {
NSLog(@"success");
} withError:^{
NSLog(@"error");
}];
}
http://download.youkuaiyun.com/detail/potato512/7485727