使用 KingFisher 获取圆形图像视图 (iOS - Swift)
Round Image View getting with KingFisher (iOS - Swift)
场景:
我有一个@IBOutlet weak var posterImageView: UIImageView!我正在使用 Kingfisher 在我的 tableView 上显示(我必须使用它)。图片来自API。所以,没关系,图像显示正常,但我想将此图像显示为一个圆圈。我在没有这个翠鸟的情况下做到了并且它有效,使用:
posterImageView.layer.cornedRadius = posterImageView.frame.size.width/2
posterImageView.clipsToBounds = true
但是对于 kingfisher,我正在这样做:
let imgStg: String = self.movies[indexPath.row].poster!
let imgURL: URL? = URL(string: imgStg)
let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)
cell.posterImageView.kf.setImage(with: imgSrc)
在这个函数中:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
所以,当我在我的 viewDidLoad() 上调用 posterImageView.layer.cornedRadius ... 时,xcode 说我的 TableViewController 上没有这样的 posterImageView。
如何使用此 kingfisher 调用自定义图像视图?
如果我理解正确,我认为您应该输入代码以在 cellForRowAtindexPath 方法而不是 viewDidLoad 上获取圆形图像,因此:
let imgStg: String = self.movies[indexPath.row].poster!
let imgURL: URL? = URL(string: imgStg)
let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)
cell.posterImageView.layer.cornedRadius = posterImageView.frame.size.width/2
cell.posterImageView.clipsToBounds = true
cell.posterImageView.kf.setImage(with: imgSrc)
编辑:记得 KingFisher 支持圆形图像:
let processor = RoundCornerImageProcessor(cornerRadius: 20)
imageView.kf.setImage(with: url, placeholder: nil, options: [.processor(processor)])
希望这两种方法中的一种适合您。
我遇到了同样的问题,然后我找到了这个解决方案,所以我想分享一下:
extension UIImageView {
func download(url: String?, rounded: Bool = true) {
guard let _url = url else {
return
}
if rounded {
let processor = ResizingImageProcessor(referenceSize: self.frame.size) |> RoundCornerImageProcessor(cornerRadius: self.frame.size.width / 2)
self.kf.setImage(with: URL(string: _url), placeholder: nil, options: [.processor(processor)])
} else {
self.kf.setImage(with: URL(string: _url))
}
}
}
这里的大秘密是这条指令:
ResizingImageProcessor(referenceSize: self.frame.size)
场景:
我有一个@IBOutlet weak var posterImageView: UIImageView!我正在使用 Kingfisher 在我的 tableView 上显示(我必须使用它)。图片来自API。所以,没关系,图像显示正常,但我想将此图像显示为一个圆圈。我在没有这个翠鸟的情况下做到了并且它有效,使用:
posterImageView.layer.cornedRadius = posterImageView.frame.size.width/2
posterImageView.clipsToBounds = true
但是对于 kingfisher,我正在这样做:
let imgStg: String = self.movies[indexPath.row].poster!
let imgURL: URL? = URL(string: imgStg)
let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)
cell.posterImageView.kf.setImage(with: imgSrc)
在这个函数中:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
所以,当我在我的 viewDidLoad() 上调用 posterImageView.layer.cornedRadius ... 时,xcode 说我的 TableViewController 上没有这样的 posterImageView。
如何使用此 kingfisher 调用自定义图像视图?
如果我理解正确,我认为您应该输入代码以在 cellForRowAtindexPath 方法而不是 viewDidLoad 上获取圆形图像,因此:
let imgStg: String = self.movies[indexPath.row].poster!
let imgURL: URL? = URL(string: imgStg)
let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)
cell.posterImageView.layer.cornedRadius = posterImageView.frame.size.width/2
cell.posterImageView.clipsToBounds = true
cell.posterImageView.kf.setImage(with: imgSrc)
编辑:记得 KingFisher 支持圆形图像:
let processor = RoundCornerImageProcessor(cornerRadius: 20)
imageView.kf.setImage(with: url, placeholder: nil, options: [.processor(processor)])
希望这两种方法中的一种适合您。
我遇到了同样的问题,然后我找到了这个解决方案,所以我想分享一下:
extension UIImageView {
func download(url: String?, rounded: Bool = true) {
guard let _url = url else {
return
}
if rounded {
let processor = ResizingImageProcessor(referenceSize: self.frame.size) |> RoundCornerImageProcessor(cornerRadius: self.frame.size.width / 2)
self.kf.setImage(with: URL(string: _url), placeholder: nil, options: [.processor(processor)])
} else {
self.kf.setImage(with: URL(string: _url))
}
}
}
这里的大秘密是这条指令:
ResizingImageProcessor(referenceSize: self.frame.size)