Find assets in library - add to a AVMutableComposition - export = crash

拜拜、爱过 提交于 2019-12-23 04:06:06

问题


I've been struggling with adding assets from the iPhone Photo Library to a AVMutableComposition and then export them. Here is what I got:

Finding the assets: (here I grab the 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];

}

Here I add a asset to my composition (I use the first object in the array for testing only.

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

} And finally I export the comp and here it crashes

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

}

Does anyone have an idea of what it might be? It crashes on

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

And I tried different presets and outputFileTypes.

Thanks

* SOLVED *


回答1:


I have to answer my own question now when I have solved. It's amazing that I've been struggling with this for a whole day and then I fix it right after posting a question :)

I changed and moved:

composition = [AVMutableComposition composition];

to:

composition = [[AVMutableComposition alloc] init];

I think I was too tired when I was working on this yesterday. Thanks guys!



来源:https://stackoverflow.com/questions/6579315/find-assets-in-library-add-to-a-avmutablecomposition-export-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!