我正在使用setImageWithURLRequest:placeholderImage:success:failure
从网址下载图片,并且图片下载正常。我遇到的问题是,当我将下载的图像设置为UIIMageView
时,没有显示任何内容,但删除了占位符。
我的代码如下:
__weak UIImageView *userCardImageView;
AFImageResponseSerializer *serializer = [[AFImageResponseSerializer alloc] init];
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"image/png"];
self.userCardImageView.imageResponseSerializer = serializer;
[self.userCardImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[userCardImageView setImage:image];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
if (error)
{
NSLog(@"Error: %@", error);
NSLog(@"Error response: %@", response);
}
}];
网址是正确的,因为当我将其复制并浏览到浏览器中时,我会得到我的图片。
有人可以告诉我如何让我的代码更新URL吗?我是否有权将UIImageView
分配给弱势财产?我这样做是因为使用self或强变量可能会导致保留周期。
答案 0 :(得分:3)
您似乎应该实现
行[userCardImageView setImage:image];
而不是
[userCardImageView setImage:[UIImage imageWithData:userCardImageData]];
在您的成功栏中
PS:如果你要设置图像的是self.userCardImageView,你宁愿声明一个弱控制器而不是弱UIImageView引用。像
这样的东西__weak __typeof(&*self)weakSelf = self
答案 1 :(得分:0)
实际上您没有正确地将下载的图像分配到图像视图。[userCardImageView setImage:[UIImage imageWithData:userCardImageData]];
,我们无需分配此变量userCardImageData
中的图像,请尝试此代码
[self.userCardImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
dispatch_async(dispatch_get_main_queue(), ^{
[userCardImageView setImage:image]; //set image in main thread , we got downloaded image as parameter in block
});
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
if (error)
{
NSLog(@"Error: %@", error);
NSLog(@"Error response: %@", response);
}
}];
希望这有帮助
答案 2 :(得分:0)
应该足够了:
[self.userCardImageView setImageWithURL:url];