iOS 图片相册调用封装
2015-07-22 18:44
417 查看
开发项目中,经常会用到调用相册,拍照获取照片等等。于是特地把这个功能封装了一下,方便以后重复使用。
使用方法如下:
self传入的是当前的UIViewController
allowEdit 是指选择图像时是否允许编辑
下面上代码
有更好的建议请留言给我,感谢!
使用方法如下:
#import "LYImagePicker.h" [[LYImagePicker sharePicker] showPickerWith:self allowEdit:NO completeBlock:^(UIImage *image) { //image就是选择到的图片,并且压缩了一下 }];
self传入的是当前的UIViewController
allowEdit 是指选择图像时是否允许编辑
下面上代码
#import <UIKit/UIKit.h> @interface LYImagePicker : NSObject<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate> + (LYImagePicker *)sharePicker; - (void)showPickerWith:(UIViewController *)viewController allowEdit:(BOOL)allowEdit completeBlock:(void (^)(UIImage *image))image; @end
#import "LYImagePicker.h" @implementation LYImagePicker{ void (^ImageBlock)(UIImage *image); UIViewController *_viewController; BOOL _allowEdit; } static LYImagePicker* _picker; + (LYImagePicker *)sharePicker{ if (!_picker) { _picker = [[LYImagePicker alloc]init]; } return _picker; } - (void)showPickerWith:(UIViewController *)viewController allowEdit:(BOOL)allowEdit completeBlock:(void (^)(UIImage *image))image{ UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",@"拍一张", nil]; [sheet showInView:viewController.view]; _allowEdit = allowEdit; _viewController = viewController; ImageBlock = image; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex < 2) { [self showPickerView:buttonIndex]; } } - (void)showPickerView:(NSInteger)buttonIndex{ UIImagePickerController* picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = _allowEdit; if (buttonIndex == 0) { picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; }else{ picker.sourceType = UIImagePickerControllerSourceTypeCamera; } [_viewController presentViewController:picker animated:YES completion:nil]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [picker dismissViewControllerAnimated:YES completion:nil]; NSData *imageData; if (_allowEdit) { imageData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerEditedImage"], 0.5); }else{ imageData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"], 0.5); } UIImage *image = [UIImage imageWithData:imageData]; ImageBlock(image); } @end
有更好的建议请留言给我,感谢!
相关文章推荐
- iOS 敏捷开发,常用的宏
- iOS 倒计时NSTimer
- iOS —— 崩溃闪退日志
- ios初步修饰符简介
- iOS可持续化集成: Jenkins + bundler + cocoapods + shenzhen + fastlane + pgyer
- IOS开发多线程GCD的学习(二)
- iOS开发之多线程
- iOS—修改AFNetworking源文件可接收text/plain的方法
- iOS开发之蓝牙4.0 BLE开发
- iOS两个强制旋转屏幕的方法
- 【iOS开发系列】用简单工厂模式理解OC反射机制
- ios MVC
- ios 自定义cell中有textField,点击textField时,防止键盘挡住textField
- 构建 iOS 风格移动 Web 应用程序的8款开发框架
- 谈谈iOS7及以上版本navigationBar带来的坑
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <三> NSArray
- ios学习笔记--(c基础题7)
- iOS DES ECB模式对称加密解密
- ios学习笔记:storyboard页面跳转