现在大部分的APP开发时都会调用系统的照相机和相册,并且系统提供UIImagePickerController这个类供我们开发使用,笔者在此之上进行了简单的封装(SZKImagePickerVC),使调用更加方便,快捷,并且实现了在沙盒中保存与读取照片。如有不足之处,还望各位读者能够批评指正,笔者定会虚心改正。
效果图:
调用方法:
在ViewController.m中调用 创建圆形按钮
- (void)viewDidLoad {
[super viewDidLoad];
_titleBt=[[UIButton alloc]initWithFrame:CGRectMake(WIDTH/2-75, 150, 150, 150)];
_titleBt.backgroundColor=[UIColor yellowColor];
_titleBt.layer.cornerRadius=75;
_titleBt.layer.masksToBounds=YES;
[_titleBt addTarget:self action:@selector(titleBtClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_titleBt];
//从沙盒中读取照片 imageName应当与保存时的name相同
UIImage *sandBoxImage=[SZKImagePickerVC loadImageFromSandbox:@"image"];
[_titleBt setBackgroundImage:sandBoxImage forState:UIControlStateNormal];
}
点击按钮弹出UIActionSheet选择框
#pragma mark----跳转到SZKImagePickerVC中
-(void)titleBtClick
{
//判断是否支持相机 模拟器去除相机功能
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开照相机",@"从相册上传" ,nil];
[sheet showInView:self.view];
}else{
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册上传" ,nil];
[sheet showInView:self.view];
}
}
实现UIActionSheetDelegate跳转到SZKImagePickerVC
#pragma mark-----UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//判断是否支持相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
switch (buttonIndex) {
case 0:{
[self presentViewController:ImagePickerStyleCamera];
}
break;
case 1:{
[self presentViewController:ImagePickerStylePhotoLibrary];
}
break;
default:
break;
}
}else{
switch (buttonIndex) {
case 0:{
[self presentViewController:ImagePickerStylePhotoLibrary];
}
break;
default:
break;
}
}
}
#pragma mark----跳转到SZKImagePickerVC
-(void)presentViewController:(imagePickerStyle)style
{
SZKImagePickerVC *picker=[[SZKImagePickerVC alloc]initWithImagePickerStyle:style];
picker.SZKDelegate=self;
[self presentViewController:picker animated:YES completion:nil];
}
实现SZKImagePickerVCDelegate代理方法 将选中之后的照片返回,并调用类方法将图片保存到沙盒
#pragma mark----SZKImagePickerVCDelegate
-(void)imageChooseFinish:(UIImage *)image
{
[_titleBt setBackgroundImage:image forState:UIControlStateNormal];
//保存到沙盒中
[SZKImagePickerVC saveImageToSandbox:image andImageNage:@"image" andResultBlock:^(BOOL success) {
NSLog(@"保存成功");
}];
}
以上就是大致的调用过程,点击按钮弹出选择框,选择照相机或者相册,但是模拟器不支持相机,就去掉了相机功能,真机上笔者亲测可以正常使用,再实现SZKImagePickerVCDelegate方法便可以获取到选中之后的image。
下面介绍一下实现的方法:
SZKImagePickerVC.h中
SZKImagePickerVC是继承于UIImagePickerController,可以调用父类的方法。
提供一个枚举用来判断是调用照相机还是相册
typedef NS_ENUM(NSUInteger,imagePickerStyle){
ImagePickerStyleCamera,
ImagePickerStylePhotoLibrary
};
SZKImagePickerVCDelegate的代理方法
@protocol SZKImagePickerVCDelegate <NSObject>
-(void)imageChooseFinish:(UIImage *)image;
@end
封装相应的接口
/**
* 保存成功回调
*
* @param success 保存成功的block
*/
typedef void(^resultBlock)(BOOL success);
@interface SZKImagePickerVC : UIImagePickerController
/**
* SZKImagePickerVCDelegate
*/
@property(nonatomic,assign)id<SZKImagePickerVCDelegate>SZKDelegate;
/**
* 初始化SZKImagePicker
*
* @param style 打开照相机或者图库
*
* @return 初始化SZKImagePicker
*/
-(instancetype)initWithImagePickerStyle:(imagePickerStyle)style;
/**
* 保存图片到沙盒
*
* @param image 要保存的图片
* @param imageName 保存的图片名称
* @param block 保存成功的值
*/
+(void)saveImageToSandbox:(UIImage *)image
andImageNage:(NSString *)imageName
andResultBlock:(resultBlock)block;
/**
* 沙盒中获取到的照片
*
* @param imageName 读取的照片名称
*
* @return 从沙盒读取到的照片
*/
+(UIImage *)loadImageFromSandbox:(NSString *)imageName;
/**
* 根据文件获取沙盒路径
*
* @param fileName 文件名称
*
* @return 文件在沙盒中的路径
*/
+(NSString *)filePath:(NSString *)fileName;
SZKImagePickerVC.m中
初始化SZKImagePickerVC
-(instancetype)initWithImagePickerStyle:(imagePickerStyle)style
{
self=[super init];
if (self) {
if (style==ImagePickerStyleCamera) {
self.sourceType=UIImagePickerControllerSourceTypeCamera;
}else if(style==ImagePickerStylePhotoLibrary){
self.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}
}
return self;
}
系统的代理方法,选中照片后将照片传入self.SZKDelegate中,供外部调用
#pragma mark---选取照片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//界面返回
[picker dismissViewControllerAnimated:YES completion:nil];
//获取编辑之后的图片
UIImage *editedImage=[info objectForKey:UIImagePickerControllerEditedImage];
//将获取的image传入代理方法中
[self.SZKDelegate imageChooseFinish:editedImage];
}
将照片保存到沙盒的实现方法,填入imageName
#pragma mark----将照片保存到沙盒
+(void)saveImageToSandbox:(UIImage *)image andImageNage:(NSString *)imageName andResultBlock:(resultBlock)block
{
//高保真压缩图片,此方法可将图片压缩,但是图片质量基本不变,第二个参数为质量参数
NSData *imageData=UIImageJPEGRepresentation(image, 0.5);
//将图片写入文件
NSString *filePath=[self filePath:imageName];
//是否保存成功
BOOL result=[imageData writeToFile:filePath atomically:YES];
//保存成功传值到blcok中
if (result) {
block(result);
}
}
根据imageName从沙盒中取出照片,图片的名称应当与保存时候的名称相同
#pragma mark----从沙盒中读取照片
+(UIImage *)loadImageFromSandbox:(NSString *)imageName
{
//获取沙盒路径
NSString *filePath=[self filePath:imageName];
//根据路径读取image
UIImage *image=[UIImage imageWithContentsOfFile:filePath];
return image;
}
根据文件名称获取沙盒路径
#pragma mark----获取沙盒路径
+(NSString *)filePath:(NSString *)fileName
{
//获取沙盒目录
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//保存文件名称
NSString *filePath=[paths[0] stringByAppendingPathComponent:fileName];
return filePath;
}
大致的实现方法就是这样,如果有不足或者错误的地方还望各位读者批评指正,可以评论留言,笔者收到后第一时间回复,也可以添加笔者的QQ/微信:790057066 ,并且已经把demo上传到了GitHub上面感兴趣的朋友,可以上去下载查看,记得star下,非常感谢。
SZKImagePickerVCdemo链接:https://github.com/18811314750/SZKImagePickerVC
笔者的其他文章:
iOS开发-利用命令行实现将本地代码上传到GitHub
http://www.jianshu.com/p/79b94add9057
iOS在线音乐播放SZKAVPlayer(基于AVPlayer的封装)
http://www.jianshu.com/p/4e0ac2898de0