iphone mkannotation: предупреждение о левом выноске после загрузки карты - PullRequest
0 голосов
/ 12 августа 2011

У меня есть загрузка 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]; 

}

Я думаю, что мое приложение все еще пытается загрузить аннотации, но я не могу понять, почему. Кроме того, я получаю некоторые предупреждения памяти при загрузке карты, поэтому, возможно, я не освобождаю некоторые объекты, какими я должен быть, я все еще новичок в том, как работает управление памятью, поэтому любые предложения будут полезны.

1 Ответ

1 голос
/ 13 августа 2011

Если вы в конечном итоге повторно используете представление аннотации, 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]; 
}
...