iOS - 调用系统相机,相册,上传图片方法封装.代码经过多次优化,封装了起来(单独一个类).这样可以大大的简化Controller的代码量.使Controller更加轻量化.
首先是.h文件中
#import <Foundation/Foundation.h>
/* ============================================================ */
// ** 宏定义单例模式方便外界调用
#define UPLOAD_IMAGE [DUX_UploadUserIcon shareUploadImage]
// ** 代理方法
@protocol DUX_UploadUserIconDelegate <NSObject>
@optional
// ** 处理图片的方法
- (void)uploadImageToServerWithImage:(UIImage *)image;
@end
/* ============================================================ */
@interface DUX_UploadUserIcon : NSObject <UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, weak) id <DUX_UploadUserIconDelegate> uploadImageDelegate;
@property(nonatomic,strong) UIViewController * fatherViewController;
// ** 单例方法
+ (DUX_UploadUserIcon *)shareUploadImage;
// ** 弹出选项窗口的方法
- (void)showActionSheetInFatherViewController:(UIViewController *)fatherVC delegate:(id<DUX_UploadUserIconDelegate>)aDelegate;
@end
接下来是.m文件中的实现
#import "DUX_UploadUserIcon.h"
static DUX_UploadUserIcon *uploadUserIcon = nil;
@implementation DUX_UploadUserIcon
/* ============================================================ */
#pragma mark - 单例方法
+ (DUX_UploadUserIcon *)shareUploadImage {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
uploadUserIcon = [[DUX_UploadUserIcon alloc] init];
});
return uploadUserIcon;
}
/* ============================================================ */
#pragma mark - 显示ActionSheet方法
- (void)showActionSheetInFatherViewController:(UIViewController *)fatherVC delegate:(id<DUX_UploadUserIconDelegate>)aDelegate {
uploadUserIcon.uploadImageDelegate = aDelegate;
self.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:nil];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"该设备没有照相机"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
}
/* ============================================================ */
#pragma mark - 图片库方法(从手机的图片库中查找图片)
- (void)fromPhotos {
UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
imagePC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePC.delegate = self;
imagePC.allowsEditing = YES;
[_fatherViewController presentViewController:imagePC animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
// ** 上传用户头像
if (self.uploadImageDelegate && [self.uploadImageDelegate respondsToSelector:@selector(uploadImageToServerWithImage:)]) {
[self.uploadImageDelegate uploadImageToServerWithImage:image];
}
}
@end
好了以上就是全部的实现方法,接下来给大家演示一下究竟如何使用!(写博客告诉别人怎样使用,提供案例是一个高尚的情怀~~~~~~~)
在你需要调用这个类的viewcontroller中首先遵守协议
viewController.m中
@interface DUX_MyViewController ()<DUX_UploadUserIconDelegate>
...你的内容...
@end
然后在一个方法中调用
- (void)openImagePickerView {
[UPLOAD_IMAGE showActionSheetInFatherViewController:self delegate:self];
}
实现代理方法
#pragma mark - 代理方法
- (void)uploadImageToServerWithImage:(UIImage *)image {
--这里得到的图片你自己去进行处理(例如:压缩啊,裁剪啊,等等的).
}
好,以上就是iOS中设置用户头像时调用系统原生的相册,相机的方法封装.大家可以打过去直接使用,也欢迎大家进行修改,提供更好的使用方法.欢迎大家一同讨论学习!