サーバーからいくつかの写真をダウンロードする方法があります。ダウンロードしたデータ用のバッファを async ブロック (NSMutableArray *imgtmp) で定義しましたが、そこから配列を取得する方法がわかりません。imgtmp の内容を返す、またはインスタンス変数を設定するために imgtmp にアクセスする最良の方法は何ですか?
Apple Block のドキュメントを見てきましたが、正しいセクションにいる必要はありません。__block
どうにかしてキーワードを使用して imgtmp を宣言する必要がありますか? 私はそれを試しましたが、 imgtmp はブロックの外ではまだ空でした。ありがとう!
編集: 作業モデルで更新されたコード
- (void) loadImages
{
// temp array for downloaded images. If all downloads complete, load into the actual image data array for tablerows
__block NSMutableArray *imgtmp = [[NSMutableArray alloc] init];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
int error = 0;
int totalitems = 0;
NSMutableArray *picbuf = [[NSMutableArray alloc] init];
for (int i=0; i < _imageURLS.count;i++)
{
NSLog(@"loading image for main image holder at index %i",i);
NSURL *mynsurl = [[NSURL alloc] initWithString:[_imageURLS objectAtIndex:i]];
NSData *imgData = [NSData dataWithContentsOfURL:mynsurl];
UIImage *img = [UIImage imageWithData:imgData];
if (img)
{
[picbuf addObject:img];
totalitems++;
}
else
{
NSLog(@"error loading img from %@", [_imageURLS objectAtIndex:i]);
error++;
}
}// for int i...
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"_loadedImages download COMPLETE");
imgtmp = picbuf;
[_tvStatus setText: [NSString stringWithFormat:@"%d objects have been retrieved", totalitems]];
NSLog (@"imgtmp contains %u images", [imgtmp count]);
});// get_main_queue
});// get_global_queue
}