2014-03-01 7 views
2

AVAssetExportSession을 사용하여 비디오를 내보내고 결과 비디오를 사용자 정의 앨범에 저장하려면 어떻게합니까 (예 : My Own Videos)? 그런 다음AVAssetExportSession을 사용하여 맞춤 앨범에 저장

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

을 그리고 거기에 동영상을 추가

답변

0

먼저 당신은 폴더를 만들어야합니다.

AVURLAsset *videoAsset = ... 

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset 
                     presetName:AVAssetExportPreset1280x720]; 

exportSession.outputURL = [[NSFileManager defaultManager] URLForInterviewWithFileName:newFileName]; 
exportSession.outputFileType = AVFileTypeMPEG4; 

[exportSession exportAsynchronouslyWithCompletionHandler:^{ ... 

을 그리고 앨범에 넣어 : 라이브러리에 자산을 저장

[folder addAsset:asset]; 

는 도움이되기를 바랍니다. 수출 세션 후

0

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

    }]; 


} 
관련 문제