2011-07-05 28 views
1

我一直在努力從iPhone照片庫添加資產到AVMutableComposition,然後導出它們。以下是我的了:查找資產庫 - 添加到AVMutableComposition - 導出=崩潰

尋找資產:(這裏我搶AVURLAsset)

-(void) findAssets { 

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos. 
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 

    // Within the group enumeration block, filter to enumerate just videos. 
    [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
    [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop){ 

     // The end of the enumeration is signaled by asset == nil. 
     if (alAsset) { 
      ALAssetRepresentation *representation = [alAsset defaultRepresentation]; 
      NSURL *url = [representation url]; 
      AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; 
      // Do something interesting with the AV asset. 

      [thumbs addObject:alAsset]; 
      [assets addObject:avAsset]; 
     }else if(alAsset == nil){ 
      [self createScroll]; 
     } 
    }]; 
} 
        failureBlock: ^(NSError *error) { 
         // Typically you should handle an error more gracefully than this. 
         NSLog(@"No groups"); 
        }]; 
[library release]; 

}

這裏我添加了一個資產來我的作文(我用的是第一物體在陣列中僅用於測試

-(void) addToCompositionWithAsset:(AVURLAsset*)_asset{ 
NSError *editError = nil; 
composition = [AVMutableComposition composition]; 
AVURLAsset* sourceAsset = [assets objectAtIndex:0]; 

Float64 inSeconds = 1.0; 
Float64 outSeconds = 2.0; 
// calculate time 
CMTime inTime = CMTimeMakeWithSeconds(inSeconds, 600); 
CMTime outTime = CMTimeMakeWithSeconds(outSeconds, 600); 
CMTime duration = CMTimeSubtract(outTime, inTime); 
CMTimeRange editRange = CMTimeRangeMake(inTime, duration); 
[composition insertTimeRange:editRange ofAsset:sourceAsset atTime:composition.duration error:&editError]; 

if (!editError) { 
    CMTimeGetSeconds (composition.duration); 
} 

} 最後我出口的補償,在這裏它崩潰

-(void)exportComposition { 
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetPassthrough]; 

NSLog (@"can export: %@", exportSession.supportedFileTypes); 

NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [dirs objectAtIndex:0]; 
NSString *exportPath = [documentsDirectoryPath stringByAppendingPathComponent:EXPORT_NAME]; 

[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; 
NSURL *exportURL = [NSURL fileURLWithPath:exportPath]; 

exportSession.outputURL = exportURL; 
exportSession.outputFileType = AVFileTypeQuickTimeMovie;//@"com.apple.quicktime-movie"; 

[exportSession exportAsynchronouslyWithCompletionHandler:^{ 
    NSLog (@"i is in your block, exportin. status is %d", 
      exportSession.status); 
    switch (exportSession.status) { 
     case AVAssetExportSessionStatusFailed: 
     case AVAssetExportSessionStatusCompleted: { 
      [self performSelectorOnMainThread:@selector (exportDone:) 
            withObject:nil 
           waitUntilDone:NO]; 
      break; 
     } 
    }; 
}]; 

}

沒有人有可能是什麼想法?它崩潰

AVAssetExportSession * exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetPassthrough];

我嘗試了不同的預設和outputFileTypes。

感謝

*解決*

回答

2

我現在來回答我的問題時,我已經解決了。令人驚訝的是我一直在爲此而努力了整整一天,然後我發佈提問:)

我改變和移動後右修復:

組成= [AVMutableComposition 組成]。

到:

組合物= [[AVMutableComposition 的alloc] INIT];

我想我昨天工作時太累了。多謝你們!