可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
}