繁体   English   中英

为什么我的MapViewController收到无法识别的选择器发送到实例异常? (iOS)

[英]Why am I getting a unrecognized selector sent to instance exception with my MapViewController? (iOS)

我正在参加Stanford 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.
}

如果传递的参数不是UIImage则在UIImageView上调用setImage时,可能会发生错误-[NSConcreteData _isResizable]: unrecognized selector sent to instance

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

修复getImageForMapViewController方法以返回UIImage

如果您需要更改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的类型为UIView 您正在尝试在UIView上设置image UIView不响应setImage方法。 设置图像后,您尝试将UIViewUIImageView ,这是一个坏习惯。 因此,您需要在将图像添加到leftCalloutAccessoryView之后,将leftCalloutAccessoryView分配为leftCalloutAccessoryView

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

UIImageView *imgView = (UIImageView *)view.leftCalloutAccessoryView;
[imgView setImage:image];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM