如何使圆角的图像,将其与BezierPath
问题描述:
拉我有一个的tableView细胞,具有图像视图。 对于该视图我创建的UIImage延伸,其中我正在与bezierPath 2矩形绘制,然后,从该图我作出的图像。如何使圆角的图像,将其与BezierPath
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!
}
所以,当我设置的图像转换为图像视图,边角不光滑,它们是模糊的:
请您咨询,如何处理呢? 在此先感谢!与此
UIGraphicsBeginImageContext(CGSize(width: frameOne.size.width, height: frameOne.size.height))
:
答
尝试更换此
UIGraphicsBeginImageContextWithOptions(frameOne.size, false, UIScreen.main.scale)
顺便说一句,你可以通过0.0
作为第三个参数,根据documentation:
规模应用于位图的因素。如果您指定的值为0.0,则缩放系数将设置为设备主屏幕的缩放系数。
非常感谢你,非常!!!! – Melany
@Melany不客气。快乐的编码! – alexburtnik
我通常使用0的规模,因为该系统计算出在该情况下,设备的屏幕的比例。 –