いくつかの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];
}
なぜこれが正しく機能しないのかわかりません。何か案は?