2011-08-12 46 views
0

我有一個MKMapview加載可能有5個註釋(現在)。它的加載性能很好,還有註釋。每個註釋都包含正確的左側和右側標註。一段時間後,但是(也許2分鐘),我經常得到一個EXC_BAD_ACCESS崩潰,對註釋的左側標註...iphone mkannotation:在地圖加載後的左側標註警告

- (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]; 

}

我想,也許我的應用程序仍試圖加載註釋,但我無法弄清楚爲什麼。以及我在加載地圖時收到一些內存警告,所以也許我不會按照我應該的方式釋放某些對象,但對於內存管理如何工作,我還是一個新的東西,所以任何建議都會有所幫助。

回答

1

如果您最終重新使用註釋視圖,則不會創建myImageView,但您會釋放它。在頂部將myImageView設置爲零。

- (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]; 
} 
+0

現在工作的很好。到目前爲止沒有崩潰:) – amanda