iOS开发中,调用系统相机或者相册上传图片方法的封装

一般上传图片的步骤:

1:创建UIActionSheet,利用其代理方法判断是调用相机还是相册
2:创建相机方法;
3:创建相册方法;
4:使用UINavigationControllerDelegate和UIImagePickerControllerDelegate的代理方法对得到的图片进行处理(上传服务器);

对以上这些繁琐步骤进行封装:

感想

在最近开发的项目中,在第一版本的时候只是有一个上传头像的需求,当时没有太注意这个对我带来的困扰(也不算是困扰吧,反正就是感觉到自己一直在做重复的事情),所以就按照上面的步骤一个一个地写到了VC中。。。
后来开发中,在很多VC中都要用到上传图片,当时心里想,这也太麻烦了,(有人会说,复制粘贴就好了啊!这个方法可以,但这样一来你的VC就会看起来非常臃肿)要是能封装成一个统一的方法该多好啊!!!

回顾

在项目不太忙的时候,我开始检查优化自己的代码;我开始考虑对上传图片方法进行封装,经过思考,我选择了用单例来实现封装。下面是我的实现方法:
1.创建单例类和单例方法.h文件:

#import 
//吧单例方法定义为宏,使用起来更方便
#define ZXUPLOAD_IMAGE [ZXUploadImage shareUploadImage]
//写了一个代理方法
@protocol ZXUploadImageDelegate 
@optional
- (void)uploadImageToServerWithImage:(UIImage *)image;
@end
@interface ZXUploadImage : NSObject < UIActionSheetDelegate,
          UINavigationControllerDelegate,
          UIImagePickerControllerDelegate>
{
//如果你调不出来UIViewController,请添加UIKit头文件
 UIViewController *_fatherViewController;
}
@property (nonatomic, weak) id  uploadImageDelegate;
//单例方法
+ (ZXUploadImage *)shareUploadImage;
//弹出选项的方法
- (void)showActionSheetInFatherViewController:(UIViewController *)fatherVC
          delegate:(id)aDelegate;

2.实现.h中的方法(.m文件)

static ZXUploadImage *zxUploadImage = nil;
@implementation ZXUploadImage
+ (ZXUploadImage *)shareUploadImage {
 static dispatch_once_t onceToken;
 dispatch_once(&onceToken, ^{
  zxUploadImage = [[ZXUploadImage alloc] init];
 });
 return zxUploadImage;
}
- (void)showActionSheetInFatherViewController:(UIViewController *)fatherVC
          delegate:(id)aDelegate {
 zxUploadImage.uploadImageDelegate = aDelegate;
 _fatherViewController = fatherVC;
 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                delegate:self
             cancelButtonTitle:@"取消"
           destructiveButtonTitle:nil
             otherButtonTitles:@"从手机相册上传", @"相机拍照", nil];
 [sheet showInView:fatherVC.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
 if (buttonIndex == 0) {
  [self fromPhotos];
 }else if (buttonIndex == 1) {
  [self createPhotoView];
 }
}
#pragma mark - 头像(相机和从相册中选择)
- (void)createPhotoView {
 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
  imagePC.sourceType                = UIImagePickerControllerSourceTypeCamera;
  imagePC.delegate                  = self;
  imagePC.allowsEditing             = YES;
  [_fatherViewController presentViewController:imagePC
           animated:YES
            completion:^{
            }];
 }else {
  UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示"
               message:@"该设备没有照相机"
              delegate:nil
              cancelButtonTitle:@"确定"
              otherButtonTitles:nil];
  [alert show];
 }
}
 //图片库方法(从手机的图片库中查找图片)
- (void)fromPhotos {
 UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
 imagePC.sourceType                = UIImagePickerControllerSourceTypePhotoLibrary;
 imagePC.delegate                  = self;
 imagePC.allowsEditing             = YES;
 [_fatherViewController presentViewController:imagePC
          animated:YES
           completion:^{
           }];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
 [picker dismissViewControllerAnimated:YES completion:^{
 }];
 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
 /**
  *  上传用户头像
  */
 if (self.uploadImageDelegate && [self.uploadImageDelegate respondsToSelector:@selector(uploadImageToServerWithImage:)]) {
  [self.uploadImageDelegate uploadImageToServerWithImage:image];
 }
}

这样封装就完成了,接下来就可以很方便的使用了。。。

使用方法

///在VC中上传头像方法中
- (void)selectPhoto {
 NSLog(@"上传头像");
 [ZXUPLOAD_IMAGE showActionSheetInFatherViewController:self delegate:self];
}
//实现代理方法即可
#pragma mark - ZXUploadImageDelegate
- (void)uploadImageToServerWithImage:(UIImage *)image {
//在这里处理得到的image
}

结束语

这样的封装极大地提高了编程效率,也简化了代码,使VC看起来更简洁。最后,欢迎大家一起交流学习,编程从点滴做起。

你可能感兴趣的:(iOS开发中,调用系统相机或者相册上传图片方法的封装)