Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用AVAssetExportSession并保存在自定义相册中_Ios_Objective C_Cocoa Touch - Fatal编程技术网

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

Ios 使用AVAssetExportSession并保存在自定义相册中,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,如何使用AVAssetExportSession导出视频并将结果视频保存在自定义相册中,例如My Own Videos?首先需要创建一个文件夹: ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; NSString *folderName = @"My Own Videos"; [library addAssetsGroupAlbumWithName:folderName

如何使用
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];
希望有帮助。

导出会话后(当您有url时)

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

    }];


}