調用系統相機和系統相冊,並保存到沙盒中

調用系統相冊或系統相機,用到的是**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[0];
    //跟文件名組合起來(使用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];

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章