美文网首页iOS Developer
调用系统相册和系统相机的封装

调用系统相册和系统相机的封装

作者: 原味丿丿咖啡Vitas | 来源:发表于2017-03-02 17:50 被阅读0次

.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];
}

相关文章

网友评论

    本文标题:调用系统相册和系统相机的封装

    本文链接:https://www.haomeiwen.com/subject/tyjkgttx.html