iphone mkannotation:地图加载后左侧标注发出警告

发布于 2024-11-29 11:33:06 字数 1617 浏览 1 评论 0原文

我有一个 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]; 

}

我想也许我的应用程序仍在尝试加载注释,但我不明白为什么。另外,加载地图时我会收到一些内存警告,所以也许我没有按照应有的方式释放一些对象,我对内存管理的工作方式仍然很陌生,所以任何建议都会有所帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2024-12-06 11:33:06

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

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]; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文