iOS 将AVAsset视频保存至沙盒

AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset: avAsset presetName:AVAssetExportPresetHighestQuality];
    NSURL *filUrl = [self getVideosURLPath:@"测试视频"];//获取路径
    session.outputURL = filUrl;//视频输出地址
    session.outputFileType = AVFileTypeMPEG4;
    [session exportAsynchronouslyWithCompletionHandler:^(void){
        dispatch_async(dispatch_get_main_queue(), ^{
           //视频导入成功
          //filUrl为本地视频地址
        });
    }];
//获取路径
- (NSURL *)getVideosURLPath:(NSString *)videoName{
    NSString *documents = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    documents = [documents stringByAppendingPathComponent:ShootVideoPath];
    documents = [self action_addFiles:documents];//判断文件是否存在
    NSString *failPath = [documents stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",videoName]];
    NSURL *filUrl = [NSURL fileURLWithPath:failPath];
    return filUrl;
}
//新建文件并返回地址
- (NSString *)action_addFiles:(NSString *)path{
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //文件夹已存在
    } else {
        //创建文件夹
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return path;
}

你可能感兴趣的:(iOS 将AVAsset视频保存至沙盒)