美文网首页
系统方法选取照片

系统方法选取照片

作者: 浩然爸 | 来源:发表于2018-01-23 17:08 被阅读1次

    ImagePickerTool.h

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    typedef NS_ENUM(NSUInteger, IMAGEPICKERTYPE) {
        IMAGEPICKERTYPE_ALBUM, //相册
        IMAGEPICKERTYPE_CAMERA, //照相机
    };
    
    @interface ImagePickerTool : NSObject
    
    //选取照片以后的回调方法
    @property (copy, nonatomic) void(^select)(UIImage *image);
    //取消选取的回调方法
    @property (copy, nonatomic) void(^cancel)();
    
    /**
     初始化方法
    
     @return imagePickerTool
     */
    + (instancetype)imagePickerTool;
    
    /**
     显示系统图片选取控制器
    
     @param type 类型(camera/album)
     @param controller 控制器
     @param select 选取的回调
     @param cancel 取消的回调
     */
    - (void)showWithType:(IMAGEPICKERTYPE)type inViewController:(UIViewController *)controller select:(void(^)(UIImage *image))select cancel:(void(^)())cancel;
    
    @end
    

    ImagePickerTool.m

    #import "ImagePickerTool.h"
    //#import "CaptureViewController.h"
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    @interface ImagePickerTool ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
    
    @property (strong, nonatomic) UIImagePickerController *imagePicker;
    
    @end
    
    @implementation ImagePickerTool
    
    + (instancetype)imagePickerTool
    {
        return [[self alloc] init];
    }
    
    - (void)showWithType:(IMAGEPICKERTYPE)type inViewController:(UIViewController *)controller select:(void (^)(UIImage *))select cancel:(void (^)())cancel
    {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.delegate = self;
        self.select = select;
        self.cancel = cancel;
        if (type == IMAGEPICKERTYPE_ALBUM) {
            _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            _imagePicker.allowsEditing = NO;
            
            [controller presentViewController:_imagePicker animated:YES completion:nil];
            
            
        }
        else
        {
            _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            _imagePicker.allowsEditing = NO;
            
            [controller presentViewController:_imagePicker animated:YES completion:nil];
        }
        
    }
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            UIImage *image = info[UIImagePickerControllerOriginalImage];
            UIImage *thumbImage = [UIImage imageWithData:UIImageJPEGRepresentation(image, 0.8f)];
            if (self.select)
                self.select(image);
        }];
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            if (self.cancel)
                self.cancel();
        }];
    }
    
    @end
    

    使用方法

    _pickerTool = [ImagePickerTool imagePickerTool];
    
    [self.pickerTool showWithType:(IMAGEPICKERTYPE_ALBUM) inViewController:self select:^(UIImage *image) {
               
            } cancel:^{
                
            }];
    

    相关文章

      网友评论

          本文标题:系统方法选取照片

          本文链接:https://www.haomeiwen.com/subject/ctasaxtx.html