UIImagePickerController之Block回调
方法使用:引入头文件
#import "UIImagePickerController+Block.h"
我这拖出来的两个属性
@property (weak, nonatomic) IBOutlet UIImageView *showImageView;
- (IBAction)openClick;
// 实用方法
- (IBAction)openClick {
UIImagePickerController_Block *imagePickerVC = [UIImagePickerController_Block defaultController];
[imagePickerVC initWith:self SourceType:UIImagePickerControllerSourceTypePhotoLibrary backBlcok:^(id obj) {
self.showImageView.image = obj;
}];
}
UIImagePickerController+Block.h文件如下
#import <UIKit/UIKit.h>
typedef void (^CallBackBlcok) (id obj);
@interface UIImagePickerController_Block : UIImagePickerController
+ (UIImagePickerController_Block *)defaultController;
/**
* @brief 选中照片回调方法.
*
* @param controller 当前控制器
*
* @param type 相片来源
*
* @param callbackblock 回调block,返回选中的image对象
*
*/
- (void)initWith:(UIViewController *)controller SourceType:(UIImagePickerControllerSourceType)type backBlcok:(CallBackBlcok)callbackblcok;
UIImagePickerController+Block.m文件如下
//
// UIImagePickerController+Block.m
// UIImagePickerController+Block
//
// Created by Apple on 15/9/24.
// Copyright © 2015年 Zhipin. All rights reserved.
//
#import "UIImagePickerController+Block.h"
#import <objc/runtime.h>
static id blockkey;
@interface UIImagePickerController_Block ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end
@implementation UIImagePickerController_Block
+ (UIImagePickerController_Block *)defaultController{
static UIImagePickerController_Block *intance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
intance = [[self alloc]init];
});
return intance;
}
#pragma mark - ============== 选中照片的回调
- (void)initWith:(UIViewController *)controller SourceType:(UIImagePickerControllerSourceType)type backBlcok:(CallBackBlcok)callbackblcok{
objc_setAssociatedObject(self, &blockkey, callbackblcok, OBJC_ASSOCIATION_COPY_NONATOMIC);
UIImagePickerController_Block *imageViewController = [UIImagePickerController_Block new];
// 设置照片来源
imageViewController.sourceType = type;
// 设置代理
imageViewController.delegate = self;
[controller presentViewController:imageViewController animated:YES completion:nil];
}
#pragma mark - ============== 选中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// UIImagePickerControllerOriginalImage 原始图片
// UIImagePickerControllerEditedImage 编辑后图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
CallBackBlcok block = (CallBackBlcok)objc_getAssociatedObject(self, &blockkey);
if (block) {
block(image);
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark - ============== 取消相册
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
@end
==================== 纯手打。。。。。。。。==================
==================================================