zoukankan      html  css  js  c++  java
  • 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

     

     

    ==================== 纯手打。。。。。。。。==================

    ==================================================

     

  • 相关阅读:
    VUE笔记-如何处理vue create demo时候,不能使用上下按键选择?
    帝国CMS之PC端上新栏目后,移动端无法同步,添加内容编辑页打开空白的处理方法
    帝国cms:迁移站点后,配置多端访问显示“访问端目录不存在”
    如何批量删除帝国CMS中同一前缀的数据表?
    宝塔插件"网站监控报表"错误日志显示大量不存在的链接,处理方法及流程
    mysql删除重复数据只保留一条
    VirtualBox 中 discuzq不能添加软链接的处理方法
    mysql8 source 导入大文件时 经常意外中断 且无法再链接断续 解决方法先设置 set names utf8;
    discuzq Virtualbox 虚拟机 在共享文件夹设置软链接 in 报错 Protocol error问题
    是的,奈学教育一周年了!
  • 原文地址:https://www.cnblogs.com/XHShare/p/4837001.html
Copyright © 2011-2022 走看看