SDWebImage是将URL转换为图像的绝佳库。它工作得很好。但是,我有以下问题
//对于imageview,我们设置为
eventImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "placeholder.png")) // but, for Image as UIColor, Showing compiler error if I use like following self.scrollView.backgroundColor = UIColor(patternImage: sd_setImage(with: URL(string: imageUrl), placeholderImage:UIImage(named: "placeholder.png")))
即使我尝试过跟随
let imageBG = sd_setImage(with: URL(string: image), placeholderImage: UIImage(named: "placeholder.png")) self.scrollView.backgroundColor = UIColor(patternImage: imageBG)
并显示以下错误
使用未解析的标识符' sd_setImage'
有人可以建议我如何解决这个问题吗?
答案 0 :(得分:0)
您需要先使用SDWebImageManager
下载图片,然后在UIColor(patterImage:)
构造函数中使用它。
请尝试以下代码:
SDWebImageManager.shared().loadImage(with: URL(string: imageUrl), options: [], progress: nil, completed: { (image, data, error, cacheType, success, url) in
DispatchQueue.main.async {
guard let image = image else {
return
}
self.scrollView.backgroundColor = UIColor(patternImage: image)
}
})
答案 1 :(得分:0)
发生这种情况是因为0b101010
UIImageView 方法(sd_setImage
应该是eventImageView
),http://php.net/manual/language.types.integer.php声明是:
UIImageView
表示init(patternImage image: UIImage)
参数为patternImage
而非 UIImage
。
要解决此问题,首先应加载图像视图,加载完成后,您可以将颜色指定为图像视图的UIImageView
:
image
或(没有占位符):
eventImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "placeholder.png")) { (image, error, cache, url) in
self.scrollView.backgroundColor = UIColor(patternImage: image)
}
答案 2 :(得分:0)
sd_setImage(with
适用于UIImageView
创建结构为 -
struct ImageSetter {
static func setImage(scrollView: UIScrollView,myImageView: UIImageView,imageUrl: URL,placeHolderImage: UIImage){
myImageView.sd_setImage(with: imageUrl, placeholderImage: placeHolderImage, options: [], completed: { (imageBackground, error, cache, url) in
scrollView.backgroundColor = UIColor.init(patternImage: imageBackground ?? UIImage(named: "place-holder-image")!)
})
}
}
使用 -
ImageSetter.setImage(scrollView: scrollView, myImageView: testImageView, imageUrl: url, placeHolderImage: image)