こんにちは、スタンフォードの iOS 開発クラスに取り組んでいます。スレッドについて質問があります。UIKit の呼び出しはメイン スレッドで処理する必要があることを理解しています。このようなものが合法かどうか疑問に思っていましたか?
- (UIImage *)mapViewController:(MapViewController *)sender imageForAnnotation:(id<MKAnnotation>)annotation {
FlickrPhotoAnnotation *fpa = (FlickrPhotoAnnotation *) annotation;
NSURL *url = [FlickrFetcher urlForPhoto:fpa.photo format:FlickrPhotoFormatSquare];
__block UIImage *image;
dispatch_queue_t downloadQ = dispatch_queue_create("download queue", NULL);
dispatch_async(downloadQ, ^{
NSData *data = [NSData dataWithContentsOfURL:url];
if (data) {
dispatch_async(dispatch_get_main_queue(), ^{
image = [UIImage imageWithData:data];
});
}
});
dispatch_release(downloadQ);
return image;
}
または、NSData を返して、呼び出し元のメソッドですべてのスレッドを処理する必要がありますか?
ありがとう