[英]Why am I getting a unrecognized selector sent to instance exception with my MapViewController? (iOS)
I'm taking the Stanford iOS class (sorry, I'm one of these guys but gotta learn somehow I guess) and I'm using code that is almost exactly the same as the code that the prof used in the lecture about MKMapViews but I'm getting this exception that he didn't and I really can't figure it out. 我正在参加Stanford iOS课(对不起,我是其中之一,但我想一定要学点知识),并且我使用的代码与教授在MKMapViews讲座中使用的代码几乎完全相同,但是我得到了他没有的例外,我真的无法弄清楚。 What could be causing this? 是什么原因造成的?
The exception I'm getting: 我得到的例外:
-[NSConcreteData _isResizable]: unrecognized selector sent to instance 0x90a4c00 -[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.
}
The error -[NSConcreteData _isResizable]: unrecognized selector sent to instance
can happen when calling setImage
on a UIImageView
if the parameter you are passing is not really a UIImage
. 如果传递的参数不是UIImage
则在UIImageView
上调用setImage
时,可能会发生错误-[NSConcreteData _isResizable]: unrecognized selector sent to instance
。
According to your comment, the getImageForMapViewController
method is actually returning NSData
instead of a UIImage
. 根据您的评论, getImageForMapViewController
方法实际上返回NSData
而不是UIImage
。 This can cause the error you are seeing. 这可能会导致您看到的错误。
Fix the getImageForMapViewController
method to return a UIImage
. 修复getImageForMapViewController
方法以返回UIImage
。
If you need to change the image of your MKPinAnnotationView
use like: 如果您需要更改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];
}
Here is the issue, Change this method: 这是问题,更改此方法:
-(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.
}
to 至
-(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.
}
Here the issue is that the leftCalloutAccessoryView
is of type UIView
. 这里的问题是leftCalloutAccessoryView
的类型为UIView
。 You are trying to set image
on UIView
. 您正在尝试在UIView
上设置image
。 UIView
don't respond to setImage
method. UIView
不响应setImage
方法。 After setting the image you are trying to cast UIView
to UIImageView
, that's a bad habit. 设置图像后,您尝试将UIView
为UIImageView
,这是一个坏习惯。 So you need to add the image to a imageView after that you need to assign the imageView as the leftCalloutAccessoryView
. 因此,您需要在将图像添加到leftCalloutAccessoryView
之后,将leftCalloutAccessoryView
分配为leftCalloutAccessoryView
。
When you are trying to write like this [(UIImageView *)view.leftCalloutAccessoryView setImage:image];
当您尝试这样写时[(UIImageView *)view.leftCalloutAccessoryView setImage:image];
remember to cast it first then call the method. 请记住先进行转换,然后再调用方法。 For the above line it's better to write like, 对于上面的行,最好这样写:
UIImageView *imgView = (UIImageView *)view.leftCalloutAccessoryView;
[imgView setImage:image];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.