通过相册和相机去获取照片(公用类)

2019-03-20  本文已影响0人  追梦小怪兽

前言:
这个需求应该有很多人都会用到。因为这个在我的几个项目中都有用到。就花了点时间来写了一个公用的类。不过遇到了几个坑,下面给大家一一说明,话不多说,show you code。

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h>


NS_ASSUME_NONNULL_BEGIN

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

@interface LTChoosePicture : NSObject

@property (nonatomic,copy) ChoosePictureCompleteBlock completeBlock;

- (void)LTChoosePictureFromAblumOrCameraWithController:(UIViewController *)viewController tipTitle:(NSString *)tipTitle completeBlock:(ChoosePictureCompleteBlock)block;

@end

NS_ASSUME_NONNULL_END
#import "LTChoosePicture.h"

@interface LTChoosePicture ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UIViewController *viewController;
@property (nonatomic,strong) UIImage *image;
@property (nonatomic,strong) NSString *titleString;

@end

@implementation LTChoosePicture

- (void)LTChoosePictureFromAblumOrCameraWithController:(UIViewController *)viewController tipTitle:(NSString *)tipTitle completeBlock:(ChoosePictureCompleteBlock)block{
    self.viewController = viewController;
    self.titleString = tipTitle;
    self.completeBlock = block;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:tipTitle message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *abulmAction = [UIAlertAction actionWithTitle:@"相册照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openAlbum];
    }];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍摄照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alert addAction:abulmAction];
    [alert addAction:cameraAction];
    [alert addAction:cancelAction];
    [viewController presentViewController:alert animated:YES completion:nil];
}

- (void)openAlbum{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self.viewController presentViewController:picker animated:YES completion:nil];
}
- (void)openCamera{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self.viewController presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    __weak typeof(self) weakSelf = self;
    [picker dismissViewControllerAnimated:YES completion:^{
        
        if (weakSelf.completeBlock){
            weakSelf.completeBlock(image);
        }
    }];
}
    self.choosePic = [[LTChoosePicture alloc] init];
    __weak typeof(self) weakSelf = self;
    [self.choosePic LTChoosePictureFromAblumOrCameraWithController:self tipTitle:@"请选择照片来源" completeBlock:^(UIImage *image) {
        NSLog(@"我来了");
        weakSelf.imageView.image = image;
    }];

---来自涛胖子的工作笔记

上一篇下一篇

猜你喜欢

热点阅读