当我们在开发一款app时,经常会获取相机或者系统相册来上传头像,或者上传证件照等单张图片获取的需求,为了避免过多的代码堆积在控制器中,并且可以方便使用,这样的调取方法可以直接封装成工具,通过代理方式传给控制器,这样方便管理和阅读代码,下边是我对于调取相机和相册进行的封装
1.创建一个NSObject, 以下是我创建的DtUploadPhoto
在DtUploadPhoto.h文件中定义属性和代理方法,方便传值
#import <Foundation/Foundation.h>
//如果你获取不到UIImage和UIViewController,请添加下边这个头文件
//#import <UIKit/UIKit.h>
@protocol DtUploadPhotoDelegate <NSObject>
@optional
- (void)uploadImageToServerWithImage:(UIImage *)image;
@end
@interface DtUploadPhoto : NSObject
@property (nonatomic, weak) id<DtUploadPhotoDelegate> uploadImageDelegate;
//单例方法
+ (DtUploadPhoto *)shareUploadImage;
//弹出选项的方法
- (void)showActionSheetInFatherViewController:(UIViewController *)fatherVC delegate:(id<DtUploadPhotoDelegate>)delegate;
@end
在DtUploadPhoto.m中实现调取系统相机和相册的方法
#import "DtUploadPhoto.h"
@interface DtUploadPhoto () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, weak) UIViewController *fatherViewController;
@end
static DtUploadPhoto *dtUploadImage = nil;
@implementation DtUploadPhoto
/* 设置单例,只初始化一次DtUploadPhoto实例 */
+ (DtUploadPhoto *)shareUploadImage {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dtUploadImage = [[DtUploadPhoto alloc] init];
});
return dtUploadImage;
}
- (void)showActionSheetInFatherViewController:(UIViewController *)fatherVC delegate:(id<DtUploadPhotoDelegate>)delegate {
dtUploadImage.uploadImageDelegate = delegate;
_fatherViewController = fatherVC;
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self snapImage];
}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self localPhoto];
}];
[actionSheet addAction:action1];
[actionSheet addAction:action2];
[actionSheet addAction:action3];
[fatherVC presentViewController:actionSheet animated:YES completion:nil];
}
- (void)snapImage {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
__block UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
ipc.allowsEditing = YES;
ipc.navigationBar.barTintColor = [UIColor whiteColor];
ipc.navigationBar.tintColor = [UIColor whiteColor];
ipc.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
[_fatherViewController presentViewController:ipc animated:YES completion:^{
ipc = nil;
}];
} else {
NSLog(@"模拟器无法打开照相机");
}
}
- (void)localPhoto {
__block UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
picker.navigationBar.barTintColor = [UIColor whiteColor];
picker.navigationBar.tintColor = [UIColor blackColor];
picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor]};
[_fatherViewController presentViewController:picker animated:YES completion:^{
picker = nil;
}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
/* 调取delegate方法,传递图片data到控制器使用 */
if (self.uploadImageDelegate && [self.uploadImageDelegate respondsToSelector:@selector(uploadImageToServerWithImage:)]) {
[self.uploadImageDelegate uploadImageToServerWithImage:selectedImage];
}
}];
}
@end
这样就封装好了调取相机和相册的代理方法,接下来就是在控制器中实现代理,在控制器中获取图片,并根据需要对图片进行压缩等操作
#import "DtUploadPhoto.h"
<DtUploadPhotoDelegate>
...
//需要启动代理的地方启动代理方法
- (void)clickAction {
//单张上传图片
[[DtUploadPhoto shareUploadImage] showActionSheetInFatherViewController:self delegate:self];
}
#pragma mark -- 单张上传图片的delegate
- (void)uploadImageToServerWithImage:(UIImage *)image {
NSLog(@"%@",image);
}
网友评论