AVFoundationErrorDomain Code=-11800, An unknown error occurred (-12780)

期望:

PHAsset 保存到沙盒指定目录

实现方法:

- (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].

完整代码如下:

//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;
}
posted @ 2019-05-14 10:44  binbins  阅读(2348)  评论(0编辑  收藏  举报