gpt4 book ai didi

ios - UICollectionViewCell - 圆形下载图像

转载 作者:行者123 更新时间:2023-11-30 12:36:27 24 4
gpt4 key购买 nike

我正在尝试使用使用 KingFisher(图像缓存库)获得的圆形图像设置 collectionView。

我不确定应该设置圆角、imageView 还是单元格本身。即使单元格是圆形的,图像似乎也填满了正方形。

到目前为止,我正在使用此代码(如果我在更改图像的正方形后没有设置它):

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
...
if let path = user.profilePictureURL {
if let url = URL(string: path) {
cell.profilePictureImageView.kf.setImage(with: url)
cell.profilePictureImageView.layer.cornerRadius = cell.profilePictureImageView.frame.width/2.0
}
}

和 ImageView :

class ProfilePicture : UIImageView {

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.commonInit()
}

override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}

func commonInit(){
self.layer.masksToBounds = false
self.layer.cornerRadius = self.layer.frame.width/2.0
self.clipsToBounds = true
}
}

但是第一次下载图像时,它们是这样的(单元格背景为蓝色)

Horizontal CollectionView

最佳答案

你可以这样使用:

extension UIImageView {

func setRounded() {
let radius = CGRectGetWidth(self.frame) / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}

然后调用如下方法:

imageView.setRounded()

关于ios - UICollectionViewCell - 圆形下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42807284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com