2011-06-23 7 views
0

ALAssetsのUIImageインスタンスを使用して、数多くのUIImageViewインスタンス(約10)を設定する必要があります。主なスレッドをロックしたくないので、できるだけバックグラウンドスレッドでやりたいALAssetからCGImageを取得するのは最も時間がかかるので、私はそれをバックグラウンドスレッドに入れたいと思います。ALAssetRepresentationからfullScreenImageをバックグラウンドスレッドで取得する

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

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

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

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

答えて

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

乾杯、

ヘンドリック

+0

のようなものを試してみてください – adriaan

関連する問題