1

generateCGImagesAsynchronouslyForTimes を使用していくつかの画像を作成しNSMutableArraygenerateCGImagesAsynchronouslyForTimes. コードブロックに入れるだけcompletionHandlerですが、複数回実行したくありません。このメソッドが終了した後、一度実行したいだけです。

編集

これが全部入ってる- (BFTask *)createImage:(NSInteger)someParameter {

AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:passsedAsset];
[imageGenerator generateCGImagesAsynchronouslyForTimes:times
                                     completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime,
                                                         AVAssetImageGeneratorResult result, NSError *error) {
    if (result == AVAssetImageGeneratorSucceeded) {
        UIImage *img = [UIImage imageWithCGImage:image];
        NSData *imgData = UIImageJPEGRepresentation(img, 1.0);
        UIImage *saveImage = [[UIImage alloc] initWithData:imgData];
        [mutaleArray addObject:saveImage];
        //I get Assigment to read only property error on line below
        completionSource.task = saveImage;
    }
]};

それを何に割り当てる必要がありますか?

4

2 に答える 2

3

私が最初に検討する 2 つのアプローチは、NSOperationQueue (空であることを検出できます) か、Bolts フレームワークを使用するより簡単な選択です。

Bolts を使用すると、すべて非同期で実行される一連のタスクを作成でき、それらが完了すると次のビットに進みます。

リンクさせてもらいます...

どうぞ... https://github.com/BoltsFramework

これは、ココアポッドを介して取得することもできるため、すべてがはるかに簡単になります.

ボルトの仕組みの例...

現時点では、非同期で画像を作成する関数があります。みたいな...さて、- (UIImage *)createImage: (id)someParameter;これでできるようになりました...

- (BFTask *)createImage:(NSInteger)someParameter
{
    BFTaskCompletionSource *completionSource = [BFTaskCompletionSource taskCompletionSource];

    //create your image asynchronously and then set the result of the task

    someAsyncMethodToCreateYourImageWithACompletionBlock...^(UIImage *createdImage){
        // add the images here...
        [self.imageArray addObject:createdImage];

        // the result doesn't need to be the image it just informs
        // that this one task is complete.
        completionSource.result = createdImage;
    }
    return completionSource.task;
}

次に、タスクを並行して実行する必要があります...

- (void)createAllTheImagesAsyncAndThenDoSomething
{
    // create the empty image array here
    self.imageArray = [NSMutableArray array];

    NSMutableArray *tasks = [NSMutableArray array];
    for (NSInteger i=0 ; i<100 ; ++i) {
        // Start this creation immediately and add its task to the list.
        [tasks addObject:[self createImage:i]];
    }
    // Return a new task that will be marked as completed when all of the created images are finished.
    [[BFTask taskForCompletionOfAllTasks:tasks] continueWithBlock:^id(BFTask *task){
        // this code will only run once all the images are created.
        // in here self.imageArray is populated with all the images.
    }
}
于 2015-04-12T20:09:59.790 に答える