前言:
這個需求應(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。這些可以方便快捷的使用相冊或相機加照片了。
---來自濤胖子的工作筆記
