通過相冊和相機去獲取照片(公用類)

前言:
這個需求應(yīng)該有很多人都會用到。因為這個在我的幾個項目中都有用到。就花了點時間來寫了一個公用的類。不過遇到了幾個坑,下面給大家一一說明,話不多說,show you code。

  • 首先:是公用類的頭文件,第一個注意點如果你沒有使用pch并且在pch中導(dǎo)入包含UIKit的類的話,一定要在這里導(dǎo)入UIKit,不然UI的類你是用不了的。通過一個block 在獲取到圖片之后回調(diào)到你使用的地方。
#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
  • 然后是實現(xiàn)文件
#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);
        }
    }];
}
  • 最后調(diào)用
    self.choosePic = [[LTChoosePicture alloc] init];
    __weak typeof(self) weakSelf = self;
    [self.choosePic LTChoosePictureFromAblumOrCameraWithController:self tipTitle:@"請選擇照片來源" completeBlock:^(UIImage *image) {
        NSLog(@"我來了");
        weakSelf.imageView.image = image;
    }];
  • 當(dāng)然我們必須在info.plist中加入權(quán)限Privacy - Camera Usage Description&&Privacy - Photo Library Usage Description。這個是必要的。

  • 當(dāng)你覺得大功告成的時候,結(jié)果會爆一個錯誤:errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
    百度一下就知道:Edit Scheme 中設(shè)置OS_ACTIVITY_MODE為disable即可解決


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

---來自濤胖子的工作筆記

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容