使用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.
})
但我收到错误:
无法转换类型'的值(UIImage!,NSError!,SDImageCacheType,URL!) - > Void'到预期的参数类型'SDExternalCompletionBlock?'
答案 0 :(得分:13)
终于解决了。
cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
// Perform operation.
})
答案 1 :(得分:10)
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
})
重要! 当有必要时,不要忘记将自我作为弱或无主(如此[自我弱] / [自己无主])发送到块中,以避免保留周期。
示例:
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
}
)
答案 2 :(得分:3)
根据您使用的框架中的typedef
:
typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL);
SDExternalCompletionBlock
由_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.
})
由于编译器知道完成块参数的类型(来自函数声明),您可以更简洁地编写代码,并且(IMO)更容易阅读,如下所示:
cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), completed: { (image, error, cacheType, imageURL) in
// Perform operation.
})
答案 3 :(得分:2)
这个也适用于swift 3:
cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"), options: []) { (image, error, imageCacheType, imageUrl) in
// Perform your operations here.
}
答案 4 :(得分:1)
cell.appIcon.sd_setImage(with: url, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (img, err, cacheType, imgURL) in
// code
})
试试这个,希望这样可以正常使用