Getting [NSURL cachePolicy]: unrecognized selector sent to instance during image loading, AFNetworking

My goal is to get the size of the uploaded image through a successful block, as shown below:

[imageView setImageWithURLRequest:[NSURL URLWithString:((ObjectA*)obj[indexPath.row]).imageUrl] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { CGFloat imageHeight = image.size.height; CGFloat imageWidth = image.size.width; NSLog(@"width of image is %f",imageWidth); NSLog(@"height of image is %f",imageHeight); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { ; } ]; 

However, I get a failure with the error shown, as shown below:

  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL cachePolicy]: unrecognized selector sent to instance 0x1edb9e70' 

Does anyone know the cause of this error. Please help if you have any ideas.

+7
source share
3 answers

The error tells you that cachePolicy (which is the NSURLRequest method) is called in the NSURL object.

The problem is that you are passing the NSURL object as the first parameter instead of the NSURLRequest object. (I am not familiar with this third-party API, but the documentation looks here )

+15
source

The problem with this code is:

 [imageView setImageWithURLRequest:[NSURL URLWithString:((ObjectA*)obj[indexPath.row]).imageUrl] 

The setImageWithURLRequest: parameter is equal to NSURLRequest , you pass NSURL . That is why it is crumbling.

Change it to:

 [imageViewsetImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:((ObjectA*)obj[indexPath.row]).imageUrl]] 
+12
source

I think the problem is in the first line

 [imageView setImageWithURLRequest:[NSURL URLWithString:imageUrl] 

setImageWithURLRequest from the signature looks like a pending " URLRequest ", while you are passing the URL .

So, create a URLRequest using a URL and pass it in and see if it works

+2
source

All Articles