美文网首页
通过相册和相机去获取照片(公用类)

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

作者: 追梦小怪兽 | 来源:发表于2019-03-20 10:05 被阅读0次

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

    • 首先:是公用类的头文件,第一个注意点如果你没有使用pch并且在pch中导入包含UIKit的类的话,一定要在这里导入UIKit,不然UI的类你是用不了的。通过一个block 在获取到图片之后回调到你使用的地方。
    #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;
        }];
    
    • 当然我们必须在info.plist中加入权限Privacy - Camera Usage Description&&Privacy - Photo Library Usage Description。这个是必要的。

    • 当你觉得大功告成的时候,结果会爆一个错误:errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
      百度一下就知道:Edit Scheme 中设置OS_ACTIVITY_MODE为disable即可解决


      08AA5B62-17E6-42B2-B138-B8D85FC2294F.png
    • OK。这些可以方便快捷的使用相册或相机加照片了。

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

    相关文章

      网友评论

          本文标题:通过相册和相机去获取照片(公用类)

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