一般上傳圖片的步驟:
1:創(chuàng)建UIActionSheet,利用其代理方法判斷是調用相機還是相冊
2:創(chuàng)建相機方法;
3:創(chuàng)建相冊方法;
4:使用UINavigationControllerDelegate和UIImagePickerControllerDelegate的代理方法對得到的圖片進行處理(上傳服務器);
對以上這些繁瑣步驟進行封裝:
感想
在最近開發(fā)的項目中,在第一版本的時候只是有一個上傳頭像的需求,當時沒有太注意這個對我?guī)淼睦_(也不算是困擾吧,反正就是感覺到自己一直在做重復的事情),所以就按照上面的步驟一個一個地寫到了VC中。。。
后來開發(fā)中,在很多VC中都要用到上傳圖片,當時心里想,這也太麻煩了,(有人會說,復制粘貼就好了??!這個方法可以,但這樣一來你的VC就會看起來非常臃腫)要是能封裝成一個統(tǒng)一的方法該多好?。。?!
回顧
在項目不太忙的時候,我開始檢查優(yōu)化自己的代碼;我開始考慮對上傳圖片方法進行封裝,經過思考,我選擇了用單例來實現(xiàn)封裝。下面是我的實現(xiàn)方法:
1.創(chuàng)建單例類和單例方法.h文件:
#import <Foundation/Foundation.h>
//吧單例方法定義為宏,使用起來更方便
#define ZXUPLOAD_IMAGE [ZXUploadImage shareUploadImage]
//寫了一個代理方法
@protocol ZXUploadImageDelegate <NSObject>
@optional
- (void)uploadImageToServerWithImage:(UIImage *)image;
@end
@interface ZXUploadImage : NSObject < UIActionSheetDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate>
{
//如果你調不出來UIViewController,請?zhí)砑覷IKit頭文件
UIViewController *_fatherViewController;
}
@property (nonatomic, weak) id <ZXUploadImageDelegate> uploadImageDelegate;
//單例方法
+ (ZXUploadImage *)shareUploadImage;
//彈出選項的方法
- (void)showActionSheetInFatherViewController:(UIViewController *)fatherVC
delegate:(id<ZXUploadImageDelegate>)aDelegate;
2.實現(xiàn).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<ZXUploadImageDelegate>)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];
}
//實現(xiàn)代理方法即可
#pragma mark - ZXUploadImageDelegate
- (void)uploadImageToServerWithImage:(UIImage *)image {
//在這里處理得到的image
}
結束語
這樣的封裝極大地提高了編程效率,也簡化了代碼,使VC看起來更簡潔。最后,歡迎大家一起交流學習,編程從點滴做起。