調用系統相冊和系統相機的封裝


.h文件

 #import <Foundation/Foundation.h>
 #import <UIKit/UIKit.h>
 //聲明代理方法
@protocol TW_uploadImageDelegate <NSObject>

@optional
pragma mark -獲取手機圖片
- (void)uploadImageDealWith:(UIImage *)image;

@end

@interface TWUploadImage :    
NSObject<UINavigationControllerDelegate,    UIImagePickerControllerDelegate>
/* 代理屬性 */
@property (weak, nonatomic) id<TW_uploadImageDelegate>   uploadDelegate;

/* 需要在此控制器上操作 */
@property (strong, nonatomic) UIViewController *fatherViewController;

/* 單例 */
+ (TWUploadImage *)shareUploadImage;

- (void)showAlertControllerInFatherViewController:(UIViewController *)fatherViewController delegate:(id<TW_uploadImageDelegate>)delegate;
@end

.m文件

#import "TWUploadImage.h"

@implementation TWUploadImage

static TWUploadImage *instance = nil;

//單例實現
+ (TWUploadImage *)shareUploadImage {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    instance = [[TWUploadImage alloc]init];
});
return instance;
}

 - (void)showAlertControllerInFatherViewController:(UIViewController *)fatherViewController delegate:(id<TW_uploadImageDelegate>)delegate {
instance.uploadDelegate = delegate;
_fatherViewController = fatherViewController;

//添加照片
UIImagePickerController *pictureController = [[UIImagePickerController alloc]init];
pictureController.delegate = self;
//創建提示框,提示選擇相冊還是相機
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"請選擇打開方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

//創建相機選項
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //設置點擊后進入相冊
    pictureController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //設置進入方式
    pictureController.allowsEditing = YES;
    pictureController.modalPresentationStyle = UIModalPresentationFullScreen;
    [_fatherViewController presentViewController:pictureController animated:YES completion:nil];
}];

//創建相冊選項
UIAlertAction *pohotoAction = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //設置點擊后進入相冊
    pictureController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pictureController.allowsEditing = YES;
    //設置進入方式
    pictureController.modalPresentationStyle = UIModalPresentationFullScreen;
    [_fatherViewController presentViewController:pictureController animated:YES completion:nil];
}];

//取消選項
UIAlertAction *canelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [_fatherViewController dismissViewControllerAnimated:YES completion:nil];
}];

//添加行為事件
[alertController addAction:cameraAction];
[alertController addAction:pohotoAction];
[alertController addAction:canelAction];

//彈出提示框
[_fatherViewController presentViewController:alertController animated:YES completion:nil];

}

#pragma mark -UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
//上傳用戶頭像
if (self.uploadDelegate && [self.uploadDelegate respondsToSelector:@selector(uploadImageDealWith:)]) {
    [self.uploadDelegate uploadImageDealWith:image];
}

}
@end

注意:

  • 記得在plist文件添加訪問權限Privacy - Photo Library Usage Description、Privacy - Camera Usage Description,否則會崩潰
  • 相機只能在真機下運行,不然也會崩潰

封裝后的使用

- (IBAction)chooseImage:(id)sender {
//調用方法
[[TWUploadImage shareUploadImage] showAlertControllerInFatherViewController:self delegate:self];
}

- (void)uploadImageDealWith:(UIImage *)image {
//實現縮放,或者直接拿來用
NSData *data = UIImageJPEGRepresentation(image, 0.2);
_iconImage.image = [UIImage imageWithData:data];
[self dismissViewControllerAnimated:YES completion:nil];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容