0

いくつかのUIImageViewインスタンス(約10)にALAssetsのUIImageインスタンスを設定する必要があります。メインスレッドをロックしたくないので、バックグラウンドスレッドで可能な限りロックしたいと思います。ALAssetからCGImageを取得するのは最も時間がかかるので、それをバックグラウンドスレッドに入れたいと思います。

私が抱えている問題は、最初の画像だけが実際に正しく読み込まれることです。他のUIImageViewインスタンスは空になります。

以下は私の(簡略化された)コードです。processAssetsメソッドは、アセットの配列を反復処理し、バックグラウンドスレッドでloadCGImageを呼び出します。このメソッドは、ALAssetからfullScreenImageを取得し、それをメインスレッドのpopulateImageViewに渡します。これを使用して、UIImageを生成し、UIImageViewにデータを入力します。

- (void)processAssets {   
    for(int i = 0; i < [assetArr count]; i++){
        ALAsset *asset = [assetArr objectAtIndex:i];
        [self performSelectorInBackground:@selector(loadCGImage:) withObject:asset];
    }
}

- (void)loadCGImage:(ALAsset *)asset
{    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    CGImageRef imgRef = CGImageRetain([[asset defaultRepresentation] fullScreenImage]);
    [self performSelectorOnMainThread:@selector(populateImageView:) withObject:imgRef waitUntilDone:YES];   
    CGImageRelease(imgRef);

    [pool release];
}

- (void)populateImageView:(CGImageRef)imgRef
{
    UIImage *img = [[UIImage imageWithCGImage:imgRef] retain];
    UIImageView *view = [[UIImageView alloc] initWithImage:image];
}

なぜこれが正しく機能しないのかわかりません。何か案は?

4

1 に答える 1

3

このようなことを試してみてください(ブロックを使用)

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    //load the fullscreenImage async
    dispatch_async(dispatch_get_main_queue(), ^{
      //assign the loaded image to the view.
    });
});

乾杯、

ヘンドリック

于 2011-07-05T06:42:50.663 に答える