Ios7 AFNetworking setImageWithURL:占位符图像,其中占位符也来自url
更新 在试图找到解决方案时,我碰巧注意到以下两个问题:Ios7 AFNetworking setImageWithURL:占位符图像,其中占位符也来自url,ios7,uiimageview,afnetworking,Ios7,Uiimageview,Afnetworking,更新 在试图找到解决方案时,我碰巧注意到以下两个问题: 我将研究使用这两篇文章,如果有用的话,我将进行更新 原创帖子 我得到了这个水平提要,每页包含1个项目。每个项目都包含一些文本信息和我从url中提取的背景图像 由于背景图像质量很高,下载可能需要一些时间,所以我想我需要一个占位符。我想使用的占位符是一个非常模糊、非常低质量的背景图像版本,这意味着我需要一个受人喜爱的AFNetworking方法的变体:[bg setImageWithURL:placeholder image::——它将
AFNetworking
方法的变体:[bg setImageWithURL:placeholder image::
——它将知道如何接受来自url的占位符(假设占位符图像的重量远小于原始尺寸的图像)
总之,我的问题是:如何从url获取图像并使用占位符,其中占位符图像也来自url?
我尝试过的事情:
- 创建另一个
,并对占位符的url使用UIImageView*占位符
方法,然后使用[placeholder setImageWithURL:
作为占位符.image
调用的占位符[bg setImageWithURL:placeholder image:
- 使用
方法,并在成功块中调用[Placeholder Image setImageWithURLRequest:Placeholder Image:success:failure:
方法[bg setImageWithURL:]
setImageWithURLRequest:placeholder图像:成功:失败:
,在成功块上,再次使用setImageWithURLRequest:placeholder图像:成功:失败:
方法-这一次为原始方法形象
我将提供我的解决方案的代码片段-可能在将来对某人有所帮助:
//Protect against retain cycle
__weak UIImageView *weakBg = bg;
//get the placeholder - note the placeholderImage parameter is nil (I don't need a placeholder to the placeholder
[bg setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:placeholderURL]]
placeholderImage: nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
UIImageView *strongBg = weakBg; // Make local strong reference to protect against race conditions
if (!strongBg) return;
//Protect against retain cycle
__weak UIImageView *weakBg = strongBg;
//Get the original bg with the low quality bg as placeholder
[strongBg setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:originalBgImageURL]]
placeholderImage:image
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
UIImageView *strongBg = weakBg; // Make local strong reference to protect against race conditions
if (!strongBg) return;
//Do stuff
} failure:NULL];
} failure:NULL];