天天看點

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.csdn.net/detail/potato512/7485727

繼續閱讀