長夜漫漫,無心睡眠。突然想著想一篇簡單的博客吧,雖然也是技術渣,算記錄一下自己成長吧!!! 記得自己剛開學習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新聞熱點
疑難解答