码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-05-14 10:58:19      阅读:524      评论:0      收藏:0      [点我收藏+]

标签:url   dde   oid   扩展   rdo   obj   fst   null   格式   

期望:

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;
}

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

标签:url   dde   oid   扩展   rdo   obj   fst   null   格式   

原文地址:https://www.cnblogs.com/binbins/p/10860960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!