Owen
Owen

Reputation: 919

MKAnnotationView image not displaying

I have an MKMapView instance in a UIView subclass which conforms to the MKMapViewDelegate protocol by implementing the viewForAnnotation:(id<MKAnnotation>) annotation method. The code of which is:

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[DriverLocation class]]) {
    MKAnnotationView* a = [self.map dequeueReusableAnnotationViewWithIdentifier:@"driverView"];
    if (a == nil) {
        MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];
        a.enabled = YES;
        a.image = [UIImage imageNamed:@"car.png"];
    }
    return a;
}
return nil;

}

The image is not loading properly - the loaded image property has a height and width of zero and the dimensions of the MKAnnotationView instance a are also zero.

The image is 4Kb png 32 pixels x 32 pixels.

I can confirm that the image has been copied into the root .app directory in the simulator.

Any help as to why this isn't loading would be appreciated!

Upvotes: 0

Views: 1568

Answers (3)

NSTJ
NSTJ

Reputation: 3888

For completeness for this post: You need to change MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];

to:

a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];

Upvotes: 2

Simon Germain
Simon Germain

Reputation: 6844

I have seen this before. What solved the problem for me was to re-export my PNGs. For some reason, some of them get metadata in them (like Fireworks stuff) which causes it to bug out in certain situations. I've seen that happen mostly with Internet Explorer, but have seen it with MKMapView as well. Even more strange, it worked in the simulator but not on the device.

Upvotes: 0

smparkes
smparkes

Reputation: 14083

Try adding [a sizeToFit] after setting the image.

Upvotes: 0

Related Questions