繁体   English   中英

使用AVAssetExportSession并保存在自定义相册中

[英]Use AVAssetExportSession and save in custom album

如何使用AVAssetExportSession导出视频并将结果视频保存在自定义相册中,例如“ My Own Videos

首先,您需要创建一个文件夹:

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

希望能帮助到你。

导出会话之后(当您拥有网址时)

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

    }];


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM