gpt4 book ai didi

ios - 如何在使用 BezierPath 绘制的图像中制作圆角

转载 作者:行者123 更新时间:2023-11-28 06:24:19 34 4
gpt4 key购买 nike

我有一个 tableView 单元格,它有一个 ImageView 。对于这个 View ,我创建了 UIImage 扩展,我在其中使用 bezierPath 2 矩形绘制,然后,我从这个绘图中制作了一个图像。

 class func drawTwoRectangles(_ frameOne: CGRect, frameTwo: CGRect, frameColor: UIColor) -> UIImage {

UIGraphicsBeginImageContext(CGSize(width: frameOne.size.width, height: frameOne.size.height))
let context = UIGraphicsGetCurrentContext()

UIColor(red: 0/255.0, green: 153/255.0, blue: 216/255.0, alpha: 1.0).setFill()

let bpath:UIBezierPath = UIBezierPath(roundedRect:CGRect(origin: CGPoint(x:1, y:1), size: CGSize(width: frameOne.size.width - 2, height: frameOne.size.height - 2)), byRoundingCorners: [.allCorners], cornerRadii: CGSize(width: 5, height: 5))
bpath.lineCapStyle = .round
bpath.lineJoinStyle = .round

bpath.close()
bpath.fill()


UIColor.white.setFill()
UIColor.white.setStroke()

let bpathTwo:UIBezierPath = UIBezierPath(rect: frameTwo)
bpathTwo.close()
bpathTwo.fill()
bpathTwo.stroke()

frameColor.setStroke()

let bpathThree:UIBezierPath = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x:1, y:1), size: CGSize(width: frameOne.size.width - 2, height: frameOne.size.height - 2)), cornerRadius: 5)
bpathThree.lineWidth = 2
bpathThree.close()

bpathThree.stroke()

bpath.append(bpathTwo)
bpath.append(bpathThree)

context?.addPath(bpath.cgPath)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return image!
}

因此,当我将此图像设置为 ImageView 时,角落不平滑,它们变得模糊:enter image description here

请问如何处理?提前致谢!

最佳答案

尝试替换这个:

UIGraphicsBeginImageContext(CGSize(width: frameOne.size.width, height: frameOne.size.height))

有了这个:

UIGraphicsBeginImageContextWithOptions(frameOne.size, false, UIScreen.main.scale)

顺便说一句,根据documentation,您可以将0.0 作为第三个参数传递。 :

The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.

关于ios - 如何在使用 BezierPath 绘制的图像中制作圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42418953/

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