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];
}
なぜこれが正しく動作しないのかわかりません。何か案は?
のようなものを試してみてください – adriaan