如何使用AVAssetExportSession
导出视频并将结果视频保存在自定义相册中,例如My Own Videos
?
答案 0 :(得分:0)
首先,您需要创建一个文件夹:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSString *folderName = @"My Own Videos";
[library addAssetsGroupAlbumWithName:folderName
resultBlock:^(ALAssetsGroup *group)
{
NSLog(@"Added folder:%@", folderName);
}
failureBlock:^(NSError *error)
{
NSLog(@"Error adding folder");
}];
然后,找到文件夹:
__block ALAssetsGroup* folder;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:folderName])
{
folder = group;
}
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
并将视频添加到其中。将资产保存到库:
AVURLAsset *videoAsset = ...
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset
presetName:AVAssetExportPreset1280x720];
exportSession.outputURL = [[NSFileManager defaultManager] URLForInterviewWithFileName:newFileName];
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{ ...
并将其放入相册:
[folder addAsset:asset];
希望它有所帮助。
答案 1 :(得分:0)
导出会话后(当你有网址时)
-(void)saveVideoUrl:(NSURL*)url toCollection:(NSString *)collectionTitle {
NSLog(@"entered %s", __PRETTY_FUNCTION__);
__block PHFetchResult *photosAsset;
__block PHAssetCollection *collection;
__block PHObjectPlaceholder *placeholder;
// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", collectionTitle];
// this is how we get a match for album Title held by 'collectionTitle'
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions].firstObject;
// check if album exists
if (!collection)
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
NSLog(@" Album did not exist, now creating album: %@",collectionTitle);
// Create the album
PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionTitle];
placeholder = [createAlbum placeholderForCreatedAssetCollection];
} completionHandler:^(BOOL didItSucceed, NSError *error) {
if (didItSucceed)
{
PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier] options:nil];
collection = collectionFetchResult.firstObject;
}
}];
}
// Save to the album
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
placeholder = [assetRequest placeholderForCreatedAsset];
photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection assets:photosAsset];
[albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL didItSucceed, NSError *error) {
if (didItSucceed)
{ // if YES
NSLog(@" Looks like Image was saved in camera Roll as %@", placeholder.localIdentifier);
NSLog(@"placeholder holds %@", placeholder.debugDescription );
}
else
{
NSLog(@"%@", error);
}
}];
}