【问题标题】:How to make rounded corners in image that was drawn with BezierPath如何在使用 BezierPath 绘制的图像中制作圆角
【发布时间】:2017-02-23 14:46:22
【问题描述】:

我有一个 tableView 单元格,它有一个图像视图。 对于这个视图,我创建了 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!
}

所以当我将此图像设置为 Image 视图时,角落不平滑,它们被模糊:

您能建议一下,如何处理吗? 提前致谢!

【问题讨论】:

    标签: ios swift uiimageview uiimage uibezierpath


    【解决方案1】:

    尝试替换这个:

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

    用这个:

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

    顺便把0.0作为第三个参数传入,根据documentation

    应用于位图的比例因子。如果您指定值 0.0,则比例因子设置为设备主屏幕的比例因子。

    【讨论】:

    • 我通常使用 0 作为比例尺,因为在这种情况下系统会计算出设备屏幕的比例尺。
    猜你喜欢
    • 2019-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2020-04-28
    • 1970-01-01
    相关资源
    最近更新 更多