【问题标题】:iphone mkannotation: warning on left callout after map has loadediphone mkannotation:地图加载后左侧标注警告
【发布时间】:2011-08-12 19:44:52
【问题描述】:

我有一个 MKMapview 加载可能有 5 个注释(现在)。它加载得很好,还有注释。每个注释都包含正确的左右标注。但是过了一段时间(可能是 2 分钟),我经常在注释的左侧标注处遇到 EXC_BAD_ACCESS 崩溃...

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

}

我在想也许我的应用仍在尝试加载注释,但我不知道为什么。同样,在加载地图时我会收到一些内存警告,所以也许我没有按照我应该的方式释放一些对象,我对内存管理的工作方式还是有点陌生​​,所以任何建议都会有所帮助。

【问题讨论】:

    标签: objective-c mkmapview mkannotation mkannotationview


    【解决方案1】:

    如果您最终重新使用注释视图,则不会创建 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]; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2012-11-24
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多