我有一个MKMapview加载也许5注释(现在)。它加载得很好,也有注解。每个注释都包含正确的左标注和右标注。然而,过了一段时间(也许2分钟),我经常得到EXC_BAD_ACCESS崩溃,在注释的左侧callout…
- (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];
}
我想也许我的应用程序仍在尝试加载注释,但我不知道为什么。当地图加载时,我也得到了一些内存警告,所以也许我没有像我应该的那样释放一些对象,我对内存管理的工作方式仍然是一种新的,所以任何建议都会有所帮助。
如果你最终重新使用注释视图,myImageView没有被创建,但你正在释放它。在顶部设置myImageView为nil。
- (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];
}