简体   繁体   English

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

[英]How to make rounded corners in image that was drawn with BezierPath

I have a tableView cell, that has an image view.我有一个 tableView 单元格,它有一个图像视图。 For this view I created UIImage extension, where I am drawing with bezierPath 2 rectangle, and then, from this drawing I make an image.对于这个视图,我创建了 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!
}

So when I set this image into an Image view, corners are not smooth, they are blurred:因此,当我将此图像设置为 Image 视图时,角落不平滑,它们变得模糊: 在此处输入图片说明

Could you please advice, how to deal with it?能否请您指教,如何处理? thanks in advance!提前致谢!

Try to replace this:尝试替换这个:

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

with this:有了这个:

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

By the way, you can pass 0.0 as third parameter, according to 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.如果指定值 0.0,则比例因子设置为设备主屏幕的比例因子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM