ImagePickerManager.h
#import <Foundation/Foundation.h>
#import<UIKit/UIKit.h>
#import<AVFoundation/AVFoundation.h>
#import<MobileCoreServices/MobileCoreServices.h>
#import<MediaPlayer/MediaPlayer.h>
NS_ASSUME_NONNULL_BEGIN
@interfaceImagePickerManager :NSObject
- (instancetype)initWithViewController:(UIViewController*)VC;
///选择图片的回调block
@property(nonatomic,copy)void(^didSelectImageBlock) (UIImage*image);
/// 相册选择器对象
@property (nonatomic,strong) UIImagePickerController *imagePicker;
///最大视频时长
@property(nonatomic,assign)NSTimeIntervalvideoMaximumDuration;
@property (nonatomic,assign) BOOL isVideo;
@end
ImagePickerManager.m
#import "ImagePickerManager.h"
@interface ImagePickerManager ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
///来源控制器
@property(nonatomic,strong)UIViewController*orginViewController;
/// 取出的图片
@property (nonatomic,strong) UIImage *tempImage;
@end
@implementationImagePickerManager
- (UIImagePickerController *)imagePicker{
if (!_imagePicker) {
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate=self;
/// 转场动画方式
// _imagePicker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
_imagePicker.allowsEditing=YES;//允许编辑
_imagePicker.videoMaximumDuration = 15 ; //视频时长默认15s
_imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
}
return _imagePicker;
}
- (void)setIsVideo:(BOOL)isVideo{
_isVideo= isVideo;
if(isVideo ==YES) {
/// 媒体类型
_imagePicker.mediaTypes = @[(NSString *)kUTTypeImage,(NSString *)kUTTypeMovie];
}else{
/// 媒体类型
_imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
}
}
- (instancetype)initWithViewController:(UIViewController*)VC{
self= [superinit];
if(self) {
self.orginViewController = VC;
}
return self;
}
#pragma mark- 快速创建一个图片选择弹出窗
- (void)quickAlertSheetPickerImage{
UIActionSheet *sheetView = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
[sheetViewshowInView:self.orginViewController.view];
}
#pragma mark-<UIActionSheetDelegate>
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"---%ld",buttonIndex);
if(buttonIndex ==0) {
///相册
[selfopenPhoto];
}elseif(buttonIndex ==1){
/// 拍照
[selfopenCamera];
}
}
#pragma mark- 打开相机
- (void)openCamera{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
return;
}
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.orginViewController presentViewController: self.imagePicker animated:YES completion:^{
NSLog(@"相机");
}];
}
#pragma mark- 打开相册
- (void)openPhoto{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
return;
}
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.orginViewController presentViewController: self.imagePicker animated:YES completion:^{
NSLog(@"相册");
}];
}
#pragma mark- <UIImagePickerControllerDelegate>
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *orginImage = info[UIImagePickerControllerOriginalImage];
self.tempImage= [selffixOrientation: orginImage];
/// 选择的图片
if(self.didSelectImageBlock){
self.didSelectImageBlock(self.tempImage);
}
///拍到的照片顺带保存到相册
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
[self saveImageToSystemPhotosAlbum];
}
[pickerdismissViewControllerAnimated:YES completion:nil];
}
#pragma mark- 拍的照片保存到系统相册
- (void)saveImageToSystemPhotosAlbum{
UIImageWriteToSavedPhotosAlbum(self.tempImage,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);
}
/// 系统指定的回调方法
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
NSString*msg =nil;
if(error !=NULL){
msg =@"保存图片失败";
}else{
msg =@"保存图片成功";
}
NSLog(@"%@",msg);
}
///矫正图片方向
- (UIImage*)fixOrientation:(UIImage*)aImage
{
if (aImage.imageOrientation == UIImageOrientationUp)
returnaImage;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (aImage.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform =CGAffineTransformRotate(transform,M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformRotate(transform,M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform,0, aImage.size.height);
transform =CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (aImage.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.height,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
default:
break;
}
CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage),0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextDrawImage(ctx, CGRectMake(0, 0, aImage.size.height, aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0, 0, aImage.size.width, aImage.size.height), aImage.CGImage);
break;
}
//创建新的image
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage* img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
returnimg;
}
@end
使用:
- (ImagePickerManager *)manager{
if(!_manager) {
_manager = [[ImagePickerManager alloc] initWithViewController:self];
}
return _manager;
}
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self) weakSelf = self;
[self.manager setDidSelectImageBlock:^(UIImage *img){
weakSelf.imageView.image= img;
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
[self.manager quickAlertSheetPickerImage];
}
网友评论