【问题标题】:AFNetworking setImageWithURL:placeholderImage where placeholder is also from a urlAFNetworking setImageWithURL:placeholderImage 其中占位符也来自 url
【发布时间】:2014-04-29 09:58:00
【问题描述】:

更新

在尝试寻找解决方案时,我碰巧注意到以下两个问题:

我将使用这两个帖子进行调查,如果有帮助会更新。

原帖

我得到了这个横向提要,每页包含 1 个项目。每个项目都包含一些文本信息和我从 url 中提取的背景图像。

由于背景图片质量很高,下载它可能需要一些时间,所以我想我需要一个占位符。我想使用的占位符是背景图像的极端模糊、质量非常低的版本,这意味着我需要心爱的AFNetworking 方法的变体:[bg setImageWithURL: placeholderImage:] - 知道如何接受来自的占位符一个网址(假设占位符图像的重量远小于原始尺寸)。

总结一下,我的问题是:如何从url获取图片并使用占位符,其中占位符图片也来自url?

我尝试过的事情:

  • 创建另一个UIImageView *placeholder 并在其上使用[placeholder setImageWithURL:] 方法和占位符的url,然后使用placeholder.image 作为[bg setImageWithURL: placeholderImage:] 调用的占位符。

  • 使用[placeHolderImage setImageWithURLRequest:placeholderImage:success:failure:]方法,并在成功块中调用[bg setImageWithURL:]方法。

【问题讨论】:

    标签: ios7 uiimageview afnetworking


    【解决方案1】:

    显然我走在了正确的轨道上,但由于缺乏关注,它没有奏效。 (保留周期有问题) 使用我在问题中提供的两个链接,我最终设法通过使用 setImageWithURLRequest:placeholderImage:success:failure: 作为占位符来解决问题,并在成功块上,再次使用 setImageWithURLRequest:placeholderImage:success:failure: 方法 - 这次是原始图像。

    我将提供我的解决方案的代码 sn-p - 将来可能会对某人有所帮助:

        //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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多