长夜漫漫,无心睡眠。突然想着想一篇简单的博客吧,虽然也是技术渣,算记录一下自己成长吧!!! 记得自己刚开学习IOS,不知道怎么获取图片,现在想想也是傻。 后来项目做得多了,重复的也多了,就不想写重复的代码了,干脆些一个简单的工具类来完成它。就是一个很简单的简化,会有很多局限,有什么好的想法,提出来,我有空肯定完善。如果有什么不对的地方,请大神们指出,改不改,我看心情… IOS10访问相册或者相机需要在工程的info.plist中加入权限,代码如下:
<key>NSAppleMusicUsageDescription</key> <string>访问媒体库资料</string> <key>NSCameraUsageDescription</key> <string>访问相机</string> <key>NSPhotoLibraryUsageDescription</key> <string>访问相册</string>准备工作结束 具体代码如下: 声明.h
//// PhotoHelper.h //// Created by 计海峰 on 16/9/20.// Copyright © 2016年 计海峰. All rights reserved.// #import <Foundation/Foundation.h>#import <UIKit/UIKit.h>//blocktypedef void(^SelectImage)(UIImage *img , NSString *path);//工具类@interface JPhotoHelper : NSObject<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>//单例+(JPhotoHelper *)shareInstance;/** 获取图片方法 @param title actionsheet标题 可为空 @param vc 基出vc @param block 回调 */-(void)getPhoto:(NSString *)title andController:(UIViewController *)vc andBlok:(SelectImage)block;//基础vc@PRoperty (nonatomic,weak) UIViewController *vc;//回调block@property (nonatomic,copy) SelectImage blok;@end具体.m
//// PhotoHelper.m //// Created by 计海峰 on 16/9/20.// Copyright © 2016年 计海峰. All rights reserved.//#import "JPhotoHelper.h"@interface JPhotoHelper(){ UIActionSheet *sheet; UIImagePickerController *imagePickerController;}@end@implementation JPhotoHelper//单例+(JPhotoHelper *)shareInstance{ static JPhotoHelper *sharedPhotoHelperInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedPhotoHelperInstance = [[JPhotoHelper alloc]init]; }); return sharedPhotoHelperInstance;}/** 获取图片方法 @param title actionsheet标题 可为空 @param vc 基出vc @param block 回调 */-(void)getPhoto:(NSString *)title andController:(UIViewController *)vc andBlok:(SelectImage)block{ self.vc = vc; self.blok = block; sheet = [[UIActionSheet alloc]init]; if (title) { [sheet setTitle:title]; } [sheet addButtonWithTitle:@"相册选取"]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) [sheet addButtonWithTitle:@"拍照"]; [sheet addButtonWithTitle:@"取消"]; [sheet setCancelButtonIndex:sheet.numberOfButtons-1]; sheet.delegate = self; [sheet showInView:[UIapplication sharedApplication].keyWindow];}#pragma make ActionSheet delegate- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = NO; if (buttonIndex == 0) { imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } if (buttonIndex < actionSheet.numberOfButtons-1) { if(buttonIndex == 1){ imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; } [self.vc presentViewController:imagePickerController animated:YES completion:nil]; }}#pragma make UIImagePickerController delegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; NSString* path = [self processImage:image]; [self.vc dismissViewControllerAnimated:YES completion:^{ if (self.blok) { self.blok(image,path); } }];}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil];}#pragma make 图片保存到本地 然后返回路径-(NSString*) processImage:(UIImage*)image { // Get a file path NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; NSString* filename = [self makeImageFilename]; // implementation omitted NSString* imagePath = [documentsDirectory stringByAppendingPathComponent:filename]; // Get the image data (blocking; around 1 second) NSData* imageData = UIImageJPEGRepresentation(image, 0.1); // Write the data to a file [imageData writeToFile:imagePath atomically:YES]; // Upload the image (non-blocking) //[self uploadImage:imageData withFilename:filename]; return imagePath;}//-(void) uploadImage:(NSData*)imageData withFilename:(NSString*)filename {// // this sends the upload job (implementation omitted) to a thread// // pool, which in this case is managed by PhoneGap// //}#pragma make 返回图片名称 如果需要在本地保存 可以尝试累加计数命名static int imageName = 0;-(NSString*)makeImageFilename { imageName++; return [NSString stringWithFormat:@"%d.jpg",imageName];}@end测试类
//// ViewController.m// TestPhoto//// Created by 计海峰 on 2017/2/10.// Copyright © 2017年 计海峰. All rights reserved.//#import "ViewController.h"#import "JPhotoHelper.h"@interface ViewController ()@property (nonatomic,strong) UIImageView *imageview;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (IBAction)push:(id)sender { __weak typeof(self) weakSelf = self; [[JPhotoHelper shareInstance]getPhoto:nil andController:self andBlok:^(UIImage *img, NSString *path) { [weakSelf dealPhoto:img andPath:path]; }];}-(void)dealPhoto:(UIImage *)img andPath:(NSString *)path{ NSLog(@"%@",path); self.imageview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 200, 200)]; [self.imageview setImage:img]; [self.view addSubview:self.imageview];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end新闻热点
疑难解答