【发布时间】:2021-10-16 03:39:10
【问题描述】:
我有一个不错的 UIImage 扩展,它可以使用更少的内存以高质量呈现圆形图像。我想使用这个扩展或者在 SwiftUI 中重新创建它,这样我就可以使用它了。问题是我对 SwiftUI 很陌生,甚至不确定它是否可能。有没有办法使用它?
这是扩展名:
extension UIImage {
class func circularImage(from image: UIImage, size: CGSize) -> UIImage? {
let scale = UIScreen.main.scale
let circleRect = CGRect(x: 0, y: 0, width: size.width * scale, height: size.height * scale)
UIGraphicsBeginImageContextWithOptions(circleRect.size, false, scale)
let circlePath = UIBezierPath(roundedRect: circleRect, cornerRadius: circleRect.size.width/2.0)
circlePath.addClip()
image.draw(in: circleRect)
if let roundImage = UIGraphicsGetImageFromCurrentImageContext() {
return roundImage
}
return nil
}
}
【问题讨论】: