2014-04-29 17 views
1

UPDATEAFNetworking setImageWithURL:placeholderImage其中佔位符也從URL

雖然試圖找到一個解決辦法我已經碰巧注意以下兩個問題:

我會調查使用這兩個職位,並將更新我它會變得有幫助。

原帖

我得到的含有每頁1個項目這個橫向進給。每個項目都包含一些文字信息和我從網址中提取的背景圖片。

由於背景圖片質量很高,可能需要一些時間下載,所以我想通了,我需要一個佔位符。我想使用的佔位符是一個極端模糊,非常低質量的背景圖像版本,這意味着我需要一個變種的心愛的AFNetworking方法:[bg setImageWithURL: placeholderImage:] - 將知道如何接受來自url的佔位符(使假設佔位符圖像權重小於原始大小1)。

綜上所述,我的問題是:如何從網址獲取圖片並使用佔位符,佔位符圖片來自url?

事情我想:

  • 創建另一個UIImageView *placeholder並在其上使用[placeholder setImageWithURL:]方法與佔位符的URL,然後使用placeholder.image作爲[bg setImageWithURL: placeholderImage:]調用的佔位符。

  • 使用[placeHolderImage setImageWithURLRequest:placeholderImage:success:failure:]方法,並在成功塊中調用[bg setImageWithURL:]方法。

回答

1

顯然我是在正確的軌道上,由於缺乏關注,它沒有奏效。 (保留週期有問題) 使用問題中提供的兩個鏈接,我最終設法通過使用setImageWithURLRequest:placeholderImage:success:failure:作爲佔位符解決了問題,並在成功塊上使用setImageWithURLRequest:placeholderImage:success:failure:方法的另一個用法 - 此時爲原始圖像。

我會提供我的解決方案的代碼片段 - 可能幫助別人的未來:

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