私は多くの URL リクエスト (約 60 個の小さな画像) を実行しており、それらを非同期的に実行し始めました。私のコードは別の画像を追加し(ダウンロードはほとんどありません)、リクエストを設定します。
リクエストが完了したら、最初に追加された場所に「データ」を配置したいのですが、画像を正しい場所に保存するために「imageLocation」をブロックに渡す方法がわかりません。
3行目を以下に置き換えましたが、これはうまくいくようですが、100%正しいわけではありません(画像がほぼ同じであるため、見分けるのは非常に困難です)。また、ブロックを宣言したところで「imageLocation」を渡すことも考えています。
これを確認できますか?
__block int imageLocation = [allImages カウント] - 1;
// Add another image to MArray
[allImages addObject:[UIImage imageNamed:@"downloading.png"]];
imageLocation = [allImages count] - 1;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setTimeoutInterval: 10.0];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data != nil && error == nil)
{
//All Worked
[allImages replaceObjectAtIndex:imageLocation withObject:[UIImage imageWithData:data]];
}
else
{
// There was an error, alert the user
[allImages replaceObjectAtIndex:imageLocation withObject:[UIImage imageNamed:@"error.png"]];
}];