[英]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
方法。 设置图像后,您尝试将UIView
为UIImageView
,这是一个坏习惯。 因此,您需要在将图像添加到leftCalloutAccessoryView
之后,将leftCalloutAccessoryView
分配为leftCalloutAccessoryView
。
当您尝试这样写时[(UIImageView *)view.leftCalloutAccessoryView setImage:image];
请记住先进行转换,然后再调用方法。 对于上面的行,最好这样写:
UIImageView *imgView = (UIImageView *)view.leftCalloutAccessoryView;
[imgView setImage:image];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.