获取[NSURL cachePolicy]:在下载图像期间发送到实例的无法识别的选择器AFNetworking

tra*_*uan 7 objective-c ios afnetworking

我的目的是尝试通过成功的块获取下载图像的大小,如下所示:

[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) {
                            ;
                        }  ];
Run Code Online (Sandbox Code Playgroud)

但是,我遇到了崩溃,显示如下错误:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL cachePolicy]: unrecognized selector sent to instance 0x1edb9e70'
Run Code Online (Sandbox Code Playgroud)

有谁知道这个错误的原因.如果您有任何想法,请帮忙

har*_*isg 15

错误告诉您cachePolicy(对象是一个NSURLRequest方法)正在调用它NSURL.

问题是您将NSURL对象作为第一个参数而不是对象传入NSURLRequest.(我不熟悉这个第三方API,但文档似乎在这里)


Mid*_* MP 12

问题是这个代码:

[imageView setImageWithURLRequest:[NSURL URLWithString:((ObjectA*)obj[indexPath.row]).imageUrl]
Run Code Online (Sandbox Code Playgroud)

参数setImageWithURLRequest:NSURLRequest,你正在通过NSURL.这就是崩溃的原因.

将其更改为:

[imageViewsetImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:((ObjectA*)obj[indexPath.row]).imageUrl]]
Run Code Online (Sandbox Code Playgroud)