I agree with the comment by @Vincent that since the images are being loaded in the background asynchronously, they have not yet been loaded when viewForAnnotation
is called and so the annotation's callout is stuck with a blank left icon.
When the image loading is finished by getDataInBackgroundWithBlock
, there is no automated signal telling the annotation view to refresh its leftCalloutAccessoryView
.
A crude workaround in this case would be to manually force the refresh of the leftCalloutAccessoryView
when the annotation is selected (which is when the callout will be displayed containing the leftCalloutAccessoryView
).
When an annotation is selected, the map view calls the didSelectAnnotationView
delegate method. Here, the crude workaround can be applied:
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[customMapAnnotation class]])
{
customMapAnnotation *fp = (customMapAnnotation *)view.annotation;
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:fp.restaurantIcon];
profileIconView.frame = CGRectMake(0,0,31,31);
view.leftCalloutAccessoryView = profileIconView;
}
}
Of course, it's possible that the image still hasn't loaded for the annotation that was selected (it's either just taking very long or the image url is invalid, etc).
What you can do is in viewForAnnotation
, if fp.restaurantIcon
is nil
, set the left icon to some generic "loading" icon that you add to the project resources. In didSelectAnnotationView
, you can also first check if fp.restaurantIcon
is still nil
and, if so, do nothing (ie. leave the icon as the generic "loading" icon).
A less crude solution might be to create a custom annotation view class that listens for an "image finished loading" message from its associated annotation and refreshes itself automatically.