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

StackOverflow https://stackoverflow.com/questions/11958605

質問

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. What could be causing this?

The exception I'm getting:

-[NSConcreteData _isResizable]: unrecognized selector sent to instance 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.

According to your comment, the getImageForMapViewController method is actually returning NSData instead of a UIImage. This can cause the error you are seeing.

Fix the getImageForMapViewController method to return a UIImage.

他のヒント

If you need to change the image of your MKPinAnnotationView use like:

-(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. You are trying to set image on UIView. UIView don't respond to setImage method. After setting the image you are trying to cast UIView to UIImageView, that's a bad habit. So you need to add the image to a imageView after that you need to assign the imageView as the leftCalloutAccessoryView.

When you are trying to write like this [(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帰属
所属していません StackOverflow
scroll top