Am using SDWebImage to download image. I want to do further operation if image is downloaded successfully.
cell.appIcon.sd_setImage(with: url, placeholderIma
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.
})
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
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
}
)
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.
}
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.
})