PHAsset 保存到沙盒指定目錄ide
- (PHImageRequestID)requestExportSessionForVideo:(PHAsset *)asset options:(nullable PHVideoRequestOptions *)options exportPreset:(NSString *)exportPreset resultHandler:(void (^)(AVAssetExportSession *__nullable exportSession, NSDictionary *__nullable info))resultHandler;
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x28208c600 {Error Domain=NSOSStatusErrorDomain Code=-12780 "(null)"}}
優先檢查路徑,AVAssetExportSession的屬性outputURL 爲[NSURL fileURLWithPath:path], 而不是 [NSURL URLWithString:path]..net
//PHAsset 的擴展方法 - (void)saveVideoDataWithFileName:(NSString *)fileName finish:(void(^)(BOOL saveOk))completion { //肯定保存路徑 NSString *directory = GVUSER.isLockedAlbum? DIRECTORY_VIDEO_LOCK : DIRECTORY_VIDEO; NSString *path = [directory stringByAppendingPathComponent:fileName]; BOOL fileExist = [FILEMANAGER fileExistsAtPath:path]; if (!fileExist) { PHVideoRequestOptions *option = [[PHVideoRequestOptions alloc] init]; option.version = PHVideoRequestOptionsVersionOriginal; option.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; option.networkAccessAllowed = YES; [PHImageManager.defaultManager requestExportSessionForVideo:self options:option exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) { exportSession.outputFileType = AVFileTypeMPEG4; exportSession.outputURL = [NSURL fileURLWithPath:path]; [exportSession exportAsynchronouslyWithCompletionHandler:^{ // 若是導出的狀態爲完成 if ([exportSession status] == AVAssetExportSessionStatusCompleted) { //保存完成 completion(YES); }else if (exportSession.status == AVAssetExportSessionStatusFailed){ NSLog(@"錯誤0----%@",exportSession.error); completion(NO); } }]; }]; }else { NSString *newFileName = fileName.fixedFileName; [self saveVideoDataWithFileName:newFileName finish:^(BOOL saveOk) { completion(saveOk); }]; } } //NSString 的擴展方法 - (NSString *)fixedFileName { NSString *newFileName; NSArray *coms = [self componentsSeparatedByString:@"."]; //有格式顯示 if (coms.count > 1) { NSString *fileFormat = coms.lastObject; // NSString *pureName = coms[coms.count-2]; NSString *dotFormat = [@"." stringByAppendingString:fileFormat]; NSString *pureName = [self stringByReplacingOccurrencesOfString:dotFormat withString:@""]; NSString *newPureName; if ([pureName hasSuffix:@")"]) { //有(*) if (pureName.length > 1) { NSString *num = [pureName substringWithRange:NSMakeRange(pureName.length-2, 1)]; NSString *newNum = [NSString stringWithFormat:@"%ld", (long)(num.integerValue+1)]; newPureName = [pureName stringByReplacingOccurrencesOfString:num withString:newNum]; }else { newPureName = [pureName stringByAppendingString:@"1"]; } newFileName = [NSString stringWithFormat:@"%@.%@", newPureName, fileFormat]; }else { newFileName = [NSString stringWithFormat:@"%@(1).%@", pureName, fileFormat]; } } //沒有格式顯示 else { newFileName = [self stringByAppendingString:@"(1)"]; } return newFileName; }