设想情况:
我有一个@IBOutlet弱var posterImageView: UIImageView!我正在使用翠鸟在我的tableView上展示(我必须使用它)。图像来自一个API。所以,没关系,图像显示的很好,但是我想把这个图像作为一个圆圈来显示。我没有翠鸟,它起了作用,用:
posterImageView.layer.cornedRadius = posterImageView.frame.size.width/2
posterImageView.clipsToBounds = true但对翠鸟我要这么做:
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所以当我给posterImageView.layer.cornedRadius打电话时..。在我的viewDidLoad()上,xcode说我的TableViewController上没有这样的posterImageView。
我怎么能用翠鸟的叫声来定制我的图像视图呢?
发布于 2017-04-23 18:24:16
如果我的理解是正确的,我认为您应该输入代码以获得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)])希望这两种方法中的一种对你有用。
发布于 2020-11-14 02:20:55
我面对同样的问题,然后我找到了这个解决方案,所以我想和大家分享:
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)https://stackoverflow.com/questions/43574681
复制相似问题