如何在Swift 3.0中使用SDWebImage完成块?

Par*_*han 9 ios sdwebimage swift3

我使用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. 
})
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

无法将类型'(UIImage!,NSError!,SDImageCacheType,URL!) - > Void'的值转换为预期的参数类型'SDExternalCompletionBlock?'

Par*_*han 16

终于解决了.

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


kor*_*gx9 11

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
})
Run Code Online (Sandbox Code Playgroud)

重要! 在必要时,不要忘记将自我作为弱或无主(如此[自我弱]/[自己无主])发送到块,以避免保留周期.

例:

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
        }
)
Run Code Online (Sandbox Code Playgroud)


Muh*_*aza 10

更新:SWIFT 5 SDWebImage 5.xx

        cell.imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
        cell.imageView.sd_setImage(with: url) { (image, error, cache, urls) in
            if (error != nil) {
                // Failed to load image
                cell.imageView.image = UIImage(named: "ico_placeholder")
            } else {
                // Successful in loading image
                cell.imageView.image = image
            }
        }
Run Code Online (Sandbox Code Playgroud)

================================================== ======================

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (img, err, cacheType, imgURL) in
     // code
}) 
Run Code Online (Sandbox Code Playgroud)

试试这个,希望这能正常工作


jja*_*tie 3

根据typedef您正在使用的框架:

typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL);
Run Code Online (Sandbox Code Playgroud)

anSDExternalCompletionBlock由 表示的可选参数组成_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. 
})
Run Code Online (Sandbox Code Playgroud)

由于编译器知道完成块参数的类型(来自函数声明),因此您可以更简洁地编写代码并且(IMO)更容易阅读,如下所示:

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