Filip Majernik
Filip Majernik

Reputation: 7810

iOS SDK Custom pin image is being changed randomly on MapView

I have an application using MapView and I am adding custom pin annotations on it. Everything works fine, but sometimes there is another image displayed and I cannot figure out why. I am getting the data from an JSON API, and they are definitely correct (the database hasn't been changed since months). This is a function I am using on my model class to get the image resource:

+ (UIImage *)pinImageForType:(VTStationType)type {

    NSString *imageName = nil;

    switch (type) {
        case kVTStationTypeOffstreetParking:
            imageName = @"PinGarage.png";
            break;

        case kVTStationTypeOnstreetParking:
            imageName = @"PinStreet.png";
            break;

        case kVTStationTypeCarwash:
            imageName = @"PinCarwash.png";
            break;

        case kVTStationTypeParkAndRide:
            imageName = @"PinPR.png";
            break;

        default:
            return nil;
        }

    NSLog(@"Image name: %@", imageName);
    return [UIImage imageNamed:imageName];
}

There is no other way in the whole code to obtain the image resource. The funny thing is, that sometimes the image just changes and I don't see a call of this method in the logs. Seems to me like that could be some kind of memory issue. Could you please help? Thanks.

EDIT

Here is viewForAnnotation delegate method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

if ([annotation isKindOfClass:[MKUserLocation class]]) {

    return nil;

} else if ([annotation isKindOfClass:[VTParkingZoneAnnotation class]]) {

    NSLog(@"viewForAnnotation - ParkingZone - %@", annotation);
    return [self parkingOrStationAnnotationView:annotation];

} else if ([annotation isKindOfClass:[VTStationAnnotation class]]) {

    NSLog(@"viewForAnnotation - Station - %@", annotation);
    return [self parkingOrStationAnnotationView:annotation];

} else if ([annotation isKindOfClass:[VTCalloutAnnotation class]]) {

    return [self calloutAnnotationView:annotation];

} else {

    return nil;

}
}

Here is the method where I am creating the view. The calloutAnnotationView is a different annotation and we can disregard that. Here is the method parkingOrStationAnnotationView:

- (MKAnnotationView *)parkingOrStationAnnotationView:(id<MKAnnotation>)annotation {

MKAnnotationView *pinView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationView"];

if (pinView) {
    pinView.annotation = annotation;
    return pinView;
}

pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"];
pinView.canShowCallout = NO;

if ([annotation isKindOfClass:[VTParkingZoneAnnotation class]]) {

    pinView.image = ((VTParkingZoneAnnotation *)annotation).parkingZone.pinImage;

} else if ([annotation isKindOfClass:[VTStationAnnotation class]]) {

    pinView.image = ((VTStationAnnotation *)annotation).station.pinImage;

}

return pinView;
}

Upvotes: 0

Views: 817

Answers (1)

Pierre Marty
Pierre Marty

Reputation: 166

In parkingOrStationAnnotationView, you should set the image even in case you reuse existing annotationView.

Upvotes: 1

Related Questions