7

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

[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) {
                            ;
                        }  ];

但是,我遇到了如下所示的错误:

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

有谁知道这个错误的原因。如果您有任何想法,请提供帮助

4

3 回答 3

15

该错误告诉您cachePolicy(这是一种NSURLRequest方法)正在NSURL对象上被调用。

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

于 2013-01-03T17:09:36.010 回答
12

问题在于此代码:

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

的参数setImageWithURLRequest:NSURLRequest,你正在传递NSURL。这就是它崩溃的原因。

将其更改为:

[imageViewsetImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:((ObjectA*)obj[indexPath.row]).imageUrl]]
于 2013-01-03T17:13:23.793 回答
2

我想问题出在第一行

[imageView setImageWithURLRequest:[NSURL URLWithString:imageUrl]

setImageWithURLRequest从签名看起来像期待“ URLRequest ”,而您正在传递一个URL

URLRequest所以用 the创建一个URL并传递它,看看它是否有效

于 2013-01-03T17:15:04.290 回答