![](/img/trans.png)
[英]AlamofireImage af_setImageWithURL in a UITableViewCell without placeholder image
[英]AlamofireImage: Can't get image in completion block from af_setImageWithURL
所有
我正在學習Swift,我正在嘗試使用AlamofireImage在UIImageView上設置圖像。 我使用以下代碼:
self.listImageView.af_setImageWithURL(
NSURL(string: list!.image!)!,
placeholderImage: nil,
filter: nil,
imageTransition: .CrossDissolve(0.5),
completion:{ image in
print(image)
}
)
並且控制台中的結果如下:
SUCCESS: <UIImage: 0x7fb0c3ec3d30>, {512, 286}
我的目標是在下載后對圖像做一些事情,但問題是我不理解完成回調的簽名,我不知道如何訪問完成塊中的圖像。 根據文檔, Result<UIImage, NSError>
是Result<UIImage, NSError>
。
我想這很簡單,但我沒有意識到。
謝謝
傳遞到完成塊的image
變量實際上是Alamofire.Response
類型,而不是獲取的底層UIImage
實例本身。
您需要更新完成塊,如下所示,以便從響應中獲取實際圖像:
self.listImageView.af_setImage(
withURL: URL(string: list!.image!)!,
placeholderImage: nil,
filter: nil,
imageTransition: .crossDissolve(0.5),
completion: { response in
print(response.value) # UIImage
print(response.error) # NSError
}
)
您可能首先要檢查response.result.isSuccess
(或他的兄弟response.result.isFailure
)以確定圖像是否已成功檢索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.