【问题标题】:Use AVAssetExportSession and save in custom album使用 AVAssetExportSession 并保存在自定义相册中
【发布时间】:2014-03-01 19:56:36
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:

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

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

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      在导出会话之后(当你有 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);
              }
      
          }];
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-16
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 1970-01-01
        • 2013-10-23
        • 2012-07-05
        • 2014-03-30
        相关资源
        最近更新 更多