現(xiàn)在大部分的APP開發(fā)時(shí)都會(huì)調(diào)用系統(tǒng)的照相機(jī)和相冊(cè),并且系統(tǒng)提供UIImagePickerController這個(gè)類供我們開發(fā)使用囤萤,筆者在此之上進(jìn)行了簡(jiǎn)單的封裝(SZKImagePickerVC)昼窗,使調(diào)用更加方便,快捷涛舍,并且實(shí)現(xiàn)了在沙盒中保存與讀取照片澄惊。如有不足之處,還望各位讀者能夠批評(píng)指正,筆者定會(huì)虛心改正掸驱。
效果圖:
調(diào)用方法:
在ViewController.m中調(diào)用 創(chuàng)建圓形按鈕
- (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應(yīng)當(dāng)與保存時(shí)的name相同
UIImage *sandBoxImage=[SZKImagePickerVC loadImageFromSandbox:@"image"];
[_titleBt setBackgroundImage:sandBoxImage forState:UIControlStateNormal];
}
點(diǎn)擊按鈕彈出UIActionSheet選擇框
#pragma mark----跳轉(zhuǎn)到SZKImagePickerVC中
-(void)titleBtClick
{
//判斷是否支持相機(jī) 模擬器去除相機(jī)功能
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打開照相機(jī)",@"從相冊(cè)上傳" ,nil];
[sheet showInView:self.view];
}else{
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"從相冊(cè)上傳" ,nil];
[sheet showInView:self.view];
}
}
實(shí)現(xiàn)UIActionSheetDelegate跳轉(zhuǎn)到SZKImagePickerVC
#pragma mark-----UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//判斷是否支持相機(jī)
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----跳轉(zhuǎn)到SZKImagePickerVC
-(void)presentViewController:(imagePickerStyle)style
{
SZKImagePickerVC *picker=[[SZKImagePickerVC alloc]initWithImagePickerStyle:style];
picker.SZKDelegate=self;
[self presentViewController:picker animated:YES completion:nil];
}
實(shí)現(xiàn)SZKImagePickerVCDelegate代理方法 將選中之后的照片返回肛搬,并調(diào)用類方法將圖片保存到沙盒
#pragma mark----SZKImagePickerVCDelegate
-(void)imageChooseFinish:(UIImage *)image
{
[_titleBt setBackgroundImage:image forState:UIControlStateNormal];
//保存到沙盒中
[SZKImagePickerVC saveImageToSandbox:image andImageNage:@"image" andResultBlock:^(BOOL success) {
NSLog(@"保存成功");
}];
}
以上就是大致的調(diào)用過程,點(diǎn)擊按鈕彈出選擇框毕贼,選擇照相機(jī)或者相冊(cè)温赔,但是模擬器不支持相機(jī),就去掉了相機(jī)功能鬼癣,真機(jī)上筆者親測(cè)可以正常使用陶贼,再實(shí)現(xiàn)SZKImagePickerVCDelegate方法便可以獲取到選中之后的image。
下面介紹一下實(shí)現(xiàn)的方法:
SZKImagePickerVC.h中
SZKImagePickerVC是繼承于UIImagePickerController待秃,可以調(diào)用父類的方法拜秧。
提供一個(gè)枚舉用來判斷是調(diào)用照相機(jī)還是相冊(cè)
typedef NS_ENUM(NSUInteger,imagePickerStyle){
ImagePickerStyleCamera,
ImagePickerStylePhotoLibrary
};
SZKImagePickerVCDelegate的代理方法
@protocol SZKImagePickerVCDelegate <NSObject>
-(void)imageChooseFinish:(UIImage *)image;
@end
封裝相應(yīng)的接口
/**
* 保存成功回調(diào)
*
* @param success 保存成功的block
*/
typedef void(^resultBlock)(BOOL success);
@interface SZKImagePickerVC : UIImagePickerController
/**
* SZKImagePickerVCDelegate
*/
@property(nonatomic,assign)id<SZKImagePickerVCDelegate>SZKDelegate;
/**
* 初始化SZKImagePicker
*
* @param style 打開照相機(jī)或者圖庫
*
* @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;
/**
* 根據(jù)文件獲取沙盒路徑
*
* @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;
}
系統(tǒng)的代理方法,選中照片后將照片傳入self.SZKDelegate中锥余,供外部調(diào)用
#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];
}
將照片保存到沙盒的實(shí)現(xiàn)方法腹纳,填入imageName
#pragma mark----將照片保存到沙盒
+(void)saveImageToSandbox:(UIImage *)image andImageNage:(NSString *)imageName andResultBlock:(resultBlock)block
{
//高保真壓縮圖片,此方法可將圖片壓縮驱犹,但是圖片質(zhì)量基本不變嘲恍,第二個(gè)參數(shù)為質(zhì)量參數(shù)
NSData *imageData=UIImageJPEGRepresentation(image, 0.5);
//將圖片寫入文件
NSString *filePath=[self filePath:imageName];
//是否保存成功
BOOL result=[imageData writeToFile:filePath atomically:YES];
//保存成功傳值到blcok中
if (result) {
block(result);
}
}
根據(jù)imageName從沙盒中取出照片,圖片的名稱應(yīng)當(dāng)與保存時(shí)候的名稱相同
#pragma mark----從沙盒中讀取照片
+(UIImage *)loadImageFromSandbox:(NSString *)imageName
{
//獲取沙盒路徑
NSString *filePath=[self filePath:imageName];
//根據(jù)路徑讀取image
UIImage *image=[UIImage imageWithContentsOfFile:filePath];
return image;
}
根據(jù)文件名稱獲取沙盒路徑
#pragma mark----獲取沙盒路徑
+(NSString *)filePath:(NSString *)fileName
{
//獲取沙盒目錄
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//保存文件名稱
NSString *filePath=[paths[0] stringByAppendingPathComponent:fileName];
return filePath;
}
大致的實(shí)現(xiàn)方法就是這樣,如果有不足或者錯(cuò)誤的地方還望各位讀者批評(píng)指正雄驹,可以評(píng)論留言佃牛,筆者收到后第一時(shí)間回復(fù),也可以添加筆者的QQ/微信:790057066 医舆,并且已經(jīng)把demo上傳到了GitHub上面感興趣的朋友俘侠,可以上去下載查看,記得star下蔬将,非常感謝爷速。
SZKImagePickerVCdemo鏈接:https://github.com/18811314750/SZKImagePickerVC
筆者的其他文章:
iOS開發(fā)-利用命令行實(shí)現(xiàn)將本地代碼上傳到GitHub
http://www.reibang.com/p/79b94add9057
iOS在線音樂播放SZKAVPlayer(基于AVPlayer的封裝)
http://www.reibang.com/p/4e0ac2898de0