2017-02-23 36 views
1

拉我有一個的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! 
} 

所以,當我設置的圖像轉換爲圖像視圖,邊角不光滑,它們是模糊的:enter image description here

請您諮詢,如何處理呢? 在此先感謝!與此

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

回答

1

嘗試更換此

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

順便說一句,你可以通過0.0作爲第三個參數,根據documentation

規模應用於位圖的因素。如果您指定的值爲0.0,則縮放係數將設置爲設備主屏幕的縮放係數。

+0

非常感謝你,非常!!!! – Melany

+0

@Melany不客氣。快樂的編碼! – alexburtnik

+2

我通常使用0的規模,因爲該系統計算出在該情況下,設備的屏幕的比例。 –