2017-02-23 14 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

あなたがアドバイスをしてもらえ、どのようにそれに対処するには?ありがとうございます。

答えて

1

この交換してみてください。これで

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

:ところで

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

を、あなたはdocumentationによると、第三パラメータとして0.0を渡すことができます。

規模ビットマップに適用する係数。 0.0の値を指定すると、倍率はデバイスのメイン画面の倍率に設定されます。

+0

ありがとうございます。 – Melany

+0

@Melanyよろしくお願いします。ハッピーコーディング! – alexburtnik

+2

私は通常、スケールで0を使用します。その場合、システムはデバイス画面の縮尺を計算します。 –

関連する問題