2017-02-23 2 views
1

J'ai une cellule tableView, qui a une vue d'image. Pour cette vue, j'ai créé l'extension UIImage, où je dessine avec le rectangle bezierPath 2, puis, à partir de ce dessin, je crée une image.Comment faire des coins arrondis dans l'image qui a été dessinée avec 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! 
} 

Alors, quand je mets cette image dans une vue de l'image, les coins ne sont pas lisses, ils sont floues: enter image description here

Pouvez-vous des conseils, comment faire face? merci d'avance!

Répondre

1

Essayez de remplacer ceci:

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

avec ceci:

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

Par ailleurs, vous pouvez passer 0.0 en tant que troisième paramètre, selon documentation:

L'échelle facteur à appliquer à l'image bitmap. Si vous spécifiez une valeur de 0.0, le facteur d'échelle est défini sur le facteur d'échelle de l'écran principal du périphérique.

+0

merci beaucoup! – Melany

+0

@Melany De rien. Codage heureux! – alexburtnik

+2

J'utilise généralement 0 pour l'échelle, puisque le système détermine l'échelle de l'écran de l'appareil dans ce cas. –