UIImagePickerController的简化使用

简化UIImagePickerController使用:封装代码块回调方法
本文介绍了一种简化UIImagePickerController使用的技巧,通过封装代码块回调方法来避免冗余,实现更高效的图片选择功能。重点阐述了如何设置属性源、处理数据源异常以及实现成功的回调和错误处理。

在使用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

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值