iphone mkannotation: warning on left callout after map has loaded
I have a MKMapview loading with maybe 5 annotations (right now). It loads fine, with the annotations as well. Each annotation contains the correct left and right callout. However after a while (maybe 2 minutes) i often get a EXC_BAD_ACCESS crash, on the left callout of an annotation...
- (MKAnnotationView *) mapView:(MKMapView *)thisMapView
viewForAnnotation:(MapAnnotations *)annotation
{
static NSString *MapIdentifier = @"MapIdentifier";
UIImageView *myImageView;
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[thisMapView dequeueReusableAnnotationViewWithIdentifier:MapIdentifier];
if(annotationView == nil)
{
NSString * id = [NSString stringWithFormat:@"%d", (annotation).tag];
NSString * postPhoto = [NSString stringWithFormat:@"id=%@",id];
NSString * hostStrPhoto = @"http://domain.com/get_image.php?";
hostStrPhoto = [hostStrPhoto stringByAppendingString:postPhoto];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStrPhoto]];
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imgData]];
myImageView.frame = CGRectMake(0,0,31,31);
annotationView =开发者_高级运维 [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:MapIdentifier] autorelease];
}
annotationView.animatesDrop=TRUE;
annotationView.canShowCallout = YES;
annotationView.leftCalloutAccessoryView = myImageView; //<--where I am getting the error, after all the annotations have loaded.
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
[myImageView autorelease];
}
I am thinking maybe my app is still trying to load annotations, but I can't figure out why. As well i am getting some memory warnings when the map is loaded, so perhaps I am not releasing some objects the way i should be, i'm still kind of new to how the memory management works, so any suggestions would be helpful.
If you end up re-using an annotation view, myImageView isn't created, yet you're releasing it. Set myImageView to nil at the top.
- (MKAnnotationView *) mapView:(MKMapView *)thisMapView viewForAnnotation:(MapAnnotations *)annotation {
static NSString *MapIdentifier = @"MapIdentifier";
UIImageView *myImageView = nil;
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[thisMapView dequeueReusableAnnotationViewWithIdentifier:MapIdentifier];
if(annotationView == nil) {
NSString * id = [NSString stringWithFormat:@"%d", (annotation).tag];
NSString * postPhoto = [NSString stringWithFormat:@"id=%@",id];
NSString * hostStrPhoto = @"http://domain.com/get_image.php?";
hostStrPhoto = [hostStrPhoto stringByAppendingString:postPhoto];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStrPhoto]];
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imgData]];
myImageView.frame = CGRectMake(0,0,31,31);
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:MapIdentifier] autorelease];
}
annotationView.animatesDrop=TRUE;
annotationView.canShowCallout = YES;
annotationView.leftCalloutAccessoryView = myImageView; //<--where I am getting the error, after all the annotations have loaded.
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
[myImageView release];
}
精彩评论