I have an application (Xcode 4.5) which downloads images from Flicker. I have a mapview which drops a pin for the location of each photo. Clicking the pin reveals an annotation which shows the name of the photo and a thumbnail of its image. Everything was working fine until I decided to download the thumbnail image off the main thread (my first attempt at multi threading). With the code I currently have, the annotation no longer reveals the thumbnail.
There are 4 methods I have set up to perform this task. The fourth method is not even being called. I am hoping someone can look at this code and point out obvious errors and/or a different way of attempting this:
In my mapview controller class:
//an annotation was selected
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
{
self.currentAnnotationView = [[MKAnnotationView alloc]init];
self.currentAnnotationView = aView;
[self.delegate mapViewController:self
imageForAnnotation:self.currentAnnotationView.annotation];
}
Using delegation, the above method calls the following in my tableview controller class:
// downloads Flickr image
- (void )mapViewController:(MapViewController *)sender imageForAnnotation:
(id<MKAnnotation>)annotation
{
FlickrPhotoAnnotation *fpa = (FlickrPhotoAnnotation *)annotation;
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr annotation image
downloader", NULL);
dispatch_async(downloadQueue, ^{
NSURL *url = [FlickrFetcher urlForPhoto:fpa.photo format:FlickrPhotoFormatSquare];
NSData *data = [NSData dataWithContentsOfURL:url];
self.thumbnailImage = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^ {
MapViewController *mvc = [[MapViewController alloc]init];
[mvc setAnnotationImage];
});
});
}
The above method calls the below message through an instance of mapview controller class:
//gets thumbnail image and sets it to the annotation view
- (void)setAnnotationImage
{
UIImage *image = [self.delegate getThumbnailImage];
[(UIImageView *)self.currentAnnotationView.leftCalloutAccessoryView setImage:image];
}
Finally, the above method uses delegation to call the below method in the table view controller class (which is not being called – don’t know why):
//returns the thumbnail image acquired in download
- (UIImage *)getThumbnailImage;
{
return self.thumbnailImage;
}
The method doesn’t get called because you are not setting the delegate of your second mapViewController:
should be:
…but you shouldn’t be making a new mapViewController, you should be setting the annotation image on your existing mapViewController (sender).
…and in your call to setAnnotationImage, you might as well pass in the UIImage (as you have that info when you pass the message), obviating the need for the last delegate callback:
change the setAnnotationImage method to receive the image:
Then you can set it in one go:
…and self.thumbnailImage might not need to be an iVar if this is the only place you need to get it…