How to use completion block using SDWebImage in Swift 3.0?

Deadly 提交于 2019-11-30 03:35:47

问题


Am using SDWebImage to download image. I want to do further operation if image is downloaded successfully.

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. 
})

But I am getting error:

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


回答1:


Finally solved.

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



回答2:


SWIFT 4 version

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

Important! Don't forget to send self as weak or unowned (like this [self weak]/[self unowned]) to the block, when it is necessary, to avoid retain cycles.

Example:

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
        }
)



回答3:


Updates: SWIFT 5 SDWebImage 5.x.x

        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
            }
        }

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

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

Try this, hope this will work fine




回答4:


According to the typedef in the framework you're using:

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

an SDExternalCompletionBlock consists of optional parameters as indicated by _Nullable. Therefor your code should be written like this:

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. 
})

Since the compiler knows the types of the completion block's parameters (from the function declaration) you can write the code more succinctly and (IMO) easier to read like this:

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



回答5:


This one also works with swift 3 :

cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), options: []) { (image, error, imageCacheType, imageUrl) in
            // Perform your operations here.
}


来源:https://stackoverflow.com/questions/40624617/how-to-use-completion-block-using-sdwebimage-in-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!