Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting [NSURL cachePolicy]: unrecognized selector sent to instance during downloading an image,AFNetworking [closed]

My purpose is trying to get the size of the downloaded image via the successful block like 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 am getting a crash with shown error like below :

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

Does anybody know the reason for this error. Please help if you have any ideas

like image 580
tranvutuan Avatar asked Jan 03 '13 17:01

tranvutuan


3 Answers

The error is telling you that cachePolicy (which is an NSURLRequest method) is being called on an NSURL object.

The problem is that you are passing in an NSURL object as the first parameter instead of an NSURLRequest object. (I'm not familiar with this 3rd party API, but the documentation appears to be here)

like image 138
harrisg Avatar answered Nov 10 '22 20:11

harrisg


Issue is with this code:

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

The parameter of setImageWithURLRequest: is NSURLRequest, you are passing NSURL. That's why it is crashing.

Change it to:

[imageViewsetImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:((ObjectA*)obj[indexPath.row]).imageUrl]]
like image 44
Midhun MP Avatar answered Nov 10 '22 20:11

Midhun MP


I guess the problem is in the first line

[imageView setImageWithURLRequest:[NSURL URLWithString:imageUrl]

setImageWithURLRequest from the signature looks like expecting a "URLRequest" whereas you are passing a URL.

So create a URLRequest with the URL and pass it and see if it is working

like image 2
Siby Avatar answered Nov 10 '22 19:11

Siby