調用系統相冊或系統相機,用到的是
**UIImagePickerController**
。要遵循兩個協定UIImagePickerControllerDelegate,UINavigationControllerDelegate。
具體代碼如下:
調用相冊:
self.imagePicker = [[UIImagePickerController alloc]init];
self.imagePicker.allowsEditing = YES;
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker animated:YES completion:nil];
在上面的sourceType分為三種:
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,//相冊
UIImagePickerControllerSourceTypeCamera,//相機
UIImagePickerControllerSourceTypeSavedPhotosAlbum//圖檔庫
} __TVOS_PROHIBITED;
當調用系統相機時,使用UIImagePickerControllerSourceTypeCamera這個類型
//處理點選拍照
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self showHUDMessage:@"相機不可用"];//這裡判斷相機可不可以用
}else{
self.imagePicker = [[UIImagePickerController alloc]init];
self.imagePicker.allowsEditing = YES;
self.imagePicker.delegate = self;
self.imagePicker.sourceType = sourceType;
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
實作代理方法:
pragma mark - **************** 選擇圖檔回調方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = info[UIImagePickerControllerEditedImage];//擷取選擇的圖檔
_lostImageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
寫入沙盒:
pragma mark 擷取沙盒中完整的檔案路徑
-(NSString *)getFilePath:(NSString *)fileName{
NSString *filePath;
//擷取沙盒Documents目錄路徑
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = docPaths[];
//跟檔案名組合起來(使用NSString的執行個體方法stringByAppendingPathComponent: 組成檔案的完整路徑)
filePath = [docPath stringByAppendingPathComponent:fileName];
NSLog(@"filePath:%@",filePath);
return filePath;
}
/**
* 将圖檔寫進沙盒
*/
-(void)writeImageToDocPathsWith:(NSString *)imageName{
//1、擷取檔案管理器(NSFileManager的類方法defaultManager:)
NSFileManager *fileManager = [NSFileManager defaultManager];
//2、檢查檔案是否存在(NSFileManager的執行個體方法fileExistsAtPath: ,參數是檔案路徑,需要調用方法擷取)
_filePath = [self getFilePath:[NSString stringWithFormat:@"%@.png",imageName]];
if (![fileManager fileExistsAtPath:_filePath]) {
//2.1、不存在此檔案的話,建立該檔案(NSFileManager的createFileAtPath: contents: attributes:執行個體方法)
[fileManager createFileAtPath:_filePath contents:nil attributes:nil];
}
//3、寫入檔案内容,建立一個NSFileHandle,相當于打開(NSFileHandle的fileHandleForWritingAtPath:類方法)
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
//4、寫入内容(擷取内容字元串,調用内容字元串的dataUsingEncoding:執行個體方法轉換成NSData類型,調用NSFileHandle的執行個體方法writeData:寫入檔案)
NSData *fileData = UIImagePNGRepresentation(_lostImageView.image); //[fileHandle truncateFileAtOffset:0];//相當于覆寫檔案
//檔案句柄指向檔案末尾,相當于給檔案追加内容
[fileHandle seekToEndOfFile];
//寫檔案
[fileHandle writeData:fileData];
//5、寫入完成之後關閉檔案(調用NSFileHandle的執行個體方法closeFile:)
[fileHandle closeFile];
}