我正在上斯坦福大学的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.
}
发布于 2012-08-14 11:31:08
如果要传递的参数实际上不是-[NSConcreteData _isResizable]: unrecognized selector sent to instance
,则在调用UIImageView
上的UIImage
时可能会发生错误UIImage
。
根据您的注释,getImageForMapViewController
方法实际上是返回NSData
而不是UIImage
。这可能导致您所看到的错误。
修正getImageForMapViewController
方法以返回UIImage
。
发布于 2012-08-14 10:54:01
如果需要更改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
类型的。您正在尝试将image
设置为UIView
。UIView
不响应setImage
方法。在设置图像之后,您尝试将UIView
转换为UIImageView
,这是一个坏习惯。因此,您需要将图像添加到imageView中,然后需要将imageView指定为leftCalloutAccessoryView
。
当您试图编写这样的[(UIImageView *)view.leftCalloutAccessoryView setImage:image];
时,请记住先转换它,然后调用该方法。对于上面的行,最好写如下,
UIImageView *imgView = (UIImageView *)view.leftCalloutAccessoryView;
[imgView setImage:image];
https://stackoverflow.com/questions/11958605
复制相似问题