2011-07-05 29 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];

我想我昨天工作时太累了。多谢你们!