1

我正在上斯坦福的 iOS 课程(抱歉,我是这些人中的一员,但我想我必须以某种方式学习)并且我使用的代码与教授在关于 MKMapViews 的讲座中使用的代码几乎完全相同,但是我得到了他没有的这个例外,我真的无法弄清楚。这可能是什么原因造成的?

我得到的例外:

-[NSConcreteData _isResizable]:无法识别的选择器发送到实例 0x90a4c00

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!aView) {
        aView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        aView.canShowCallout=YES;
        aView.leftCalloutAccessoryView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        aView.rightCalloutAccessoryView= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    aView.annotation=annotation;
    [(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];
    return aView;
}

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];
    [(UIImageView *)view.leftCalloutAccessoryView setImage:image]; // this is where I get the exception.
}
4

2 回答 2

5

如果您传递的参数不是真正的 a ,则在调用a-[NSConcreteData _isResizable]: unrecognized selector sent to instance时可能会发生错误。setImageUIImageViewUIImage

根据您的评论,该getImageForMapViewController方法实际上是返回NSData而不是UIImage. 这可能会导致您看到的错误。

修复getImageForMapViewController返回 a 的方法UIImage

于 2012-08-14T19:31:08.343 回答
1

如果您需要更改MKPinAnnotationView使用的图像,例如:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    MKAnnotation *pin = view.annotation;
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];

    UIImageView *imagePin = [[UIImageView alloc] initWithImage:image];
   [[mapView viewForAnnotation:pin] addSubview:imagePin];
}

这是问题,更改此方法:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];
    [(UIImageView *)view.leftCalloutAccessoryView setImage:image]; // this is where I get the exception.
}

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    UIImage *image = [self.delegate getImageForMapViewController:self withAnnotation:view.annotation];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    view.leftCalloutAccessoryView = imageView; // this is where I get the exception.
}

这里的问题是leftCalloutAccessoryView是 type UIView。您正在尝试设置image。不响应方法。将您尝试投射的图像设置为 后,这是一个坏习惯。因此,您需要将图像添加到 imageView 之后,您需要将 imageView 分配为.UIViewUIViewsetImageUIViewUIImageViewleftCalloutAccessoryView

当您尝试这样编写时,[(UIImageView *)view.leftCalloutAccessoryView setImage:image];请记住先转换它然后调用该方法。对于上面的行,最好这样写,

UIImageView *imgView = (UIImageView *)view.leftCalloutAccessoryView;
[imgView setImage:image];
于 2012-08-14T18:54:01.540 回答