Kingfisher 3.0 iOS swift圆角图片处理器白色背景。

5

这是我正在使用的代码,用于设置用户图像,它可以很好地获取和设置图片。但是图片周围有一个白色轮廓。有没有办法使其透明?

let processor = RoundCornerImageProcessor(cornerRadius: 50)
self.userPhoto.backgroundColor = Colors.accent
self.userPhoto.kf.setImage(with: url, placeholder: UIImage(named: "ic_camera_alt_48pt"), options: [.processor(processor)])

使用50的示例 输入图像说明


1
尝试:self.userPhoto.kf.clipsToBounds = true - KKRocks
@KKRocks,我没有在 Kingfisher 上看到 clipsToBounds 属性,只有图片。我尝试了 self.userPhoto.clipsToBounds = true,但并没有成功。 - ajonp
1
图像必须是带有 alpha 通道的 PNG 格式,才能实现您想要的效果。 - Eric Aya
2个回答

6
原始图像的格式不支持透明度(即JPEG)。在缓存之前,您必须将图像转换为支持透明度的格式(即PNG)。
来自Kingfisher Cheat Sheet
默认情况下(DefaultCacheSerializer),Kingfisher将尊重输入图像格式并尝试保持其不变。但是,有一些例外情况。一个常见情况是当您使用RoundCornerImageProcessor时,您可能总是希望使用alpha通道。如果您的原始图像是jpg,则可能需要设置png序列化程序:
let roundCorner = RoundCornerImageProcessor(cornerRadius: 20)
imageView.kf.setImage(with: url, 
    options: [.processor(roundCorner), 
              .cacheSerializer(FormatIndicatedCacheSerializer.png)]
)

这是被接受的答案,只是补充一下,如果您同时使用ResizingImageProcessor和RoundCornerImageProcessor,您可能需要在UIImageView上使用clipToBounds=true。 - bra.Scene

2

您不需要使用RoundCornerImageProcessor

self.userPhoto.layerCornerRadius = 50
self.userPhoto.clipsToBounds = true
self.userPhoto.backgroundColor = Colors.accent
self.userPhoto.kf.setImage(with: url, placeholder: UIImage(named: "ic_camera_alt_48pt"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接