0

ALAssetLibrary からビデオを取得して、それを処理できるようにしようとしています。私はそれを行うためにブロックを使用しています:

NSMutableArray *assets = [[NSMutableArray alloc] init];

library = [[ALAssetsLibrary alloc] init];

NSLog(@"library allocated");

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    NSLog(@"Begin enmeration");

    [group setAssetsFilter:[ALAssetsFilter allVideos]];

    NSLog(@"Filter by videos");

    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]

                            options:0

                         usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                             NSLog(@"Asset retrieved");

                             if (alAsset) {

                                 ALAssetRepresentation *representation = [alAsset defaultRepresentation];

                                 NSURL *url = [representation url];

                                 AVAsset *recentVideo = [AVURLAsset URLAssetWithURL:url options:nil];

                                 [assets addObject:recentVideo];

                                 NSLog(@"Asset added to array");

                             } 
                         }];
}

AVMutableComposition *composition = [[AVMutableComposition alloc] init];

NSLog(@"creating source");
AVURLAsset* sourceAsset = [assets objectAtIndex:0];

コードを実行すると、ブロックがスキップされ、配列内の要素にアクセスしようとすると、存在しないためにプログラムがクラッシュします。ブロックが非同期だからだと言われましたが、他のすべてよりも先にブロックを実行する方法がわかりません。performSelectorOnMainThread はそれができるように聞こえますが、これをどのように行うかを説明するものは実際には見つかりません。

4

1 に答える 1

0

お望みならば

AVMutableComposition *composition = [[AVMutableComposition alloc] init];

NSLog(@"creating source");
AVURLAsset* sourceAsset = [assets objectAtIndex:0];

の列挙後に発生しgroup、最初のブロック内に配置しますが、その後の列挙:

AVMutableComposition *composition = [[AVMutableComposition alloc] init];
__block AVURLAsset* sourceAsset = nil;
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // snip...

    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]
                            options:0
                         usingBlock:^{
                             // snip... 
                         }];
    sourceAsset = [assets objectAtIndex:0];
    // Now do other things that depend on sourceAsset being set 
}];

__blockキーワードを使用すると、ポインタをブロック内の新しいオブジェクトに設定できます。それ以外の場合、変数を再割り当てすることはできません。

于 2012-07-31T23:48:17.860 に答える