簡體   English   中英

如何在用 BezierPath 繪制的圖像中制作圓角

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

我有一個 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 視圖時,角落不平滑,它們變得模糊: 在此處輸入圖片說明

能否請您指教,如何處理? 提前致謝!

嘗試替換這個:

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

有了這個:

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

順便說一句,根據文檔,您可以將0.0作為第三個參數傳遞:

應用於位圖的比例因子。 如果指定值 0.0,則比例因子設置為設備主屏幕的比例因子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM