封装相机功能->>>>>block返回image

来源:互联网 发布:删除淘宝中差评的公司 编辑:程序博客网 时间:2024/06/06 02:40

.h

#import <Foundation/Foundation.h>

@import UIKit;


typedef void(^Completion)(UIImage *image);


@interface ZSLImagePicker : NSObject


@property (copy,nonatomic) Completion completion;

- (void)completion:(Completion)completion;


+ (ZSLImagePicker *)defaultImagePicker;


- (void)startPickImageWithVC:(UIViewController *)vc;


@end




.m

#import "ZSLImagePicker.h"


@interface ZSLImagePicker () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>


@end



@implementation ZSLImagePicker


#pragma mark - public

+ (ZSLImagePicker *)defaultImagePicker

{

    staticZSLImagePicker *imagePicker =nil;

    staticdispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        imagePicker = [selfnew];

    });

    return imagePicker;

}


- (void)startPickImageWithVC:(UIViewController *)vc

{

    UIAlertController *ac = [UIAlertControlleralertControllerWithTitle:nilmessage:@"添加照片"preferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {

        NSLog(@"cancelAction");

    }];

    

    UIAlertAction *takePhoto = [UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

        NSLog(@"takePhoto");

        

        if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            UIImagePickerController *imagePC = [UIImagePickerControllernew];

            imagePC.delegate =self;

            imagePC.editing =YES;

            imagePC.sourceType =UIImagePickerControllerSourceTypeCamera;

            

            [vc presentViewController:imagePCanimated:YEScompletion:nil];

        }else {

            NSLog(@"无相机");

        }

    }];

    

    UIAlertAction *selectPhoto = [UIAlertActionactionWithTitle:@"从相册选择 "style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

        NSLog(@"selectPhoto");

        

        UIImagePickerController *ipc = [UIImagePickerControllernew];

        ipc.delegate = (id)self;

        ipc.allowsEditing =YES;

        ipc.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

        [vc presentViewController:ipcanimated:YEScompletion:nil];

    }];

    [ac addAction:cancelAction];

    [ac addAction:takePhoto];

    [ac addAction:selectPhoto];

    [vc presentViewController:acanimated:YEScompletion:nil];

}


- (void)completion:(Completion)completion

{

    self.completion = completion;

}


#pragma mark - <UIImagePickerControllerDelegate>

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info

{

    NSString *mediaType = info[UIImagePickerControllerMediaType];

    if ([mediaTypeisEqualToString:@"public.image"]) {

        //得到照片

        UIImage *image = info[UIImagePickerControllerOriginalImage];

        self.completion(image);

    }

    

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}


@end


调用

#import "ViewController.h"

#import "ZSLImagePicker.h"


@interface ViewController ()


#pragma mark IB outlet

@property (weak,nonatomic) IBOutletUIImageView *imageView;


@end



@implementation ViewController


#pragma mark - IB action

- (IBAction)pickImage:(UIBarButtonItem *)sender

{

    ZSLImagePicker *imagePicker = [ZSLImagePickerdefaultImagePicker];

    [imagePicker startPickImageWithVC:self];

    [imagePicker completion:^(UIImage *image) {

        NSLog(@"aaa%@",image);

        self.imageView.image = image;

    }];

}


@end




0 0