简体   繁体   中英

How to use completion block using SDWebImage in Swift 3.0?

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?'

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

SWIFT 4-5 version

let url = URL(string: "http://some_url")
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
        }
)

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM