2016-11-16 101 views
3

正在使用SDWebImage下載圖像。如果圖像下載成功,我想進一步操作。如何在Swift 3.0中使用使用SDWebImage的完成塊?

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage.init(named: "App-Default"), completed: {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: URL!) -> Void in 
     // Perform operation. 
}) 

但我得到的錯誤:

Cannot convert value of type '(UIImage!, NSError!, SDImageCacheType, URL!) -> Void' to expected argument type 'SDExternalCompletionBlock?'

+0

我得到這個錯誤...無法將類型的價值 - 預期參數類型 '>虛空(UIImage的!NSError!SDImageCacheType,網址!)'「SDExternalCompletionBlock? – Parvezkhan

+2

這不應該是一個評論,應貼有你的問題 – Rajat

+0

我的代碼是:cell.appIcon.sd_setImage(附:URL,placeholderImage:UIImage.init(命名爲:「應用程序,默認」),完成:{(圖像:UIImage!,error:NSError!,cacheType:SDImageCacheType,imageURL:URL!) - > //執行操作 }) – Parvezkhan

回答

3

根據框架中typedef你使用:

typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL); 

SDExternalCompletionBlock包括可選參數通過_Nullable所示。爲此您的代碼應該這樣寫:

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage.init(named: "App-Default"), completed: {(image: UIImage?, error: NSError?, cacheType: SDImageCacheType, imageURL: URL?) -> Void in 
     // Perform operation. 
}) 

由於編譯器知道完成塊的參數的類型(從函數聲明),你可以更簡潔地編寫代碼和(IMO)更易於閱讀這樣的:

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), completed: { (image, error, cacheType, imageURL) in 
     // Perform operation. 
}) 
+0

我已經實現第一one..Getting error..Cannot調用 'sd_setImage' 類型的參數列表「(附:URL,placeholderImage:UIImage的?,完成:(UIImage的?NSError?SDImageCacheType,URL) - >無效)' – Parvezkhan

+0

如果實現第二個..獲取錯誤..'sd_setImage'的明確用法' – Parvezkhan

+0

自動完成給出cell.appIcon.sd_setImage(與:URL ?, placeholderImage:UIImage ?,完成:SDExternalCompletionBlock?) – Parvezkhan

9

終於解決了。

cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in 
// Perform operation. 
}) 
1

這其中還與SWIFT 3:

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), options: []) { (image, error, imageCacheType, imageUrl) in 
      // Perform your operations here. 
} 
0
cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (img, err, cacheType, imgURL) in 
    // code 
}) 

試試這個,希望這將正常工作

0

SWIFT 4版本

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { image, error, cacheType, imageURL in 
    // your rest code 
}) 

重要! 不要忘記發送自弱或無主的(這樣[自弱]/[無主的自])到塊,當它是必要的,以避免保留週期。

例子:

cell.appIcon.sd_setImage(
    with: url, 
    placeholderImage: UIImage(named: "App-Default"), 
    options: SDWebImageOptions(rawValue: 0), 
    completed: { [self weak] image, error, cacheType, imageURL in 
        guard let selfNotNil = self else { return } 
        // your rest code 
     } 
) 
相關問題