我有一些代码将视频文件导出到相机胶卷。如何将相同的文件保存到documents目录?
self.exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
self.exportSession.outputURL = url;
self.exportSession.outputFileType = AVFileTypeQuickTimeMovie;
self.exportSession.shouldOptimizeForNetworkUse = YES;
self.exportSession.videoComposition = videoComposition;
self.exportProgressBarTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self.delegate selector:@selector(updateProgress) userInfo:nil repeats:YES];
__block id weakSelf = self;
[self.exportSession exportAsynchronouslyWithCompletionHandler:^{ NSLog (@"i is in your block, exportin. status is %ld",(long)self.exportSession.status);
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf exportDidFinish:self.exportSession withCompletionBlock:completion];
});
}];
修改您的exportAsynchronouslyWithCompletionHandler
// it checks different completion states and at the end it stores to `Camera role`
[exporter exportAsynchronouslyWithCompletionHandler:^{
BOOL success = false;
switch ([exporter status]) {
case AVAssetExportSessionStatusCompleted:
success = true;
NSLog(@"Export Completed");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"Export Waiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"Export Exporting");
break;
case AVAssetExportSessionStatusFailed:
{
NSError *error = [exporter error];
NSLog(@"Export failed: %@", [error localizedDescription]);
break;
}
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
default:
break;
}
if (success == true) {
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
NSError *removeError = nil;
[[NSFileManager defaultManager] removeItemAtURL:url error:&removeError];
}];
}
}];
-(void)save_photo_video_document:(NSURL *)Asset_url :(NSString *)Document_path
{
self.view.userInteractionEnabled=NO;
self.navigationController.view.userInteractionEnabled=NO;
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setFrame:CGRectMake(120, 200, 80, 80)];
[spinner.layer setCornerRadius:10.0 ];
[spinner setBackgroundColor:[UIColor blueColor]];
[spinner startAnimating];
[self.view addSubview:spinner];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation * rep = [myasset defaultRepresentation];
long long bufferSize=1024*1024;
FILE* f = fopen([Document_path cStringUsingEncoding:1], "wb+");
if (f == NULL) {
return;
}
Byte * buffer = (Byte*)malloc(bufferSize);
int read = 0, offset = 0, written = 0;
NSError* err;
if (buffer != 0) {
do {
read = [rep getBytes:buffer fromOffset:offset length:bufferSize error:&err];
written = fwrite(buffer, sizeof(char), read, f);
offset += read;
} while (read != 0);
}
fclose(f);
free(buffer);
dispatch_async(dispatch_get_main_queue(), ^{
self.view.userInteractionEnabled=YES;
[spinner stopAnimating];
});
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
};
if(Asset_url)
{
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:Asset_url resultBlock:resultblock failureBlock:failureblock];
}
});
}