在iOS上绘制圆角图像时遇到奇怪的锯齿图案

问题描述:

我正尝试使用圆角创建缩略图图像,并在iOS图像底部创建一个条形图。在iOS上绘制圆角图像时遇到奇怪的锯齿图案

而且我有以下代码:

extension UIImage { 
    func thumbnails(with options: SomeDrawingOptions) -> UIImage? { 
    // ... 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { 
     UIGraphicsEndImageContext() 
    } 
    let context = UIGraphicsGetCurrentContext()! 

    UIColor.white.setFill() 
    context.fill(targetRect) 

    UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip() 

    // Drawing image 
    draw(in: targetRect) 

    // Drawing a transparent mask 
    UIColor.black.withAlphaComponent(0.4).setFill() 
    context.fill(targetRect) 

    // Drawing bar 
    UIColor.white.setFill() 
    context.fill(someBarRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
    } 
} 

最后我有锋利圆角就像那些在下面的快照图像。

Sharp Rounded Corners

任何建议,以消除圆角的锯齿?

=======溶液======

感谢@ wj2061答案,问题就迎刃而解了。这里是代码:

extension UIImage { 
    func thumbnails(with options: SomeDrawingOptions) -> UIImage? { 
    // ... 

    let targetRect = options.targetRect 
    let clippedPath = UIBezierPath(roundedRect: targetRect, cornerRadius: 8) 

    func makeImage() -> UIImage? { 
     UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
     defer { UIGraphicsEndImageContext() } 
     let context = UIGraphicsGetCurrentContext()! 

     draw(in: targetRect) 

     // Drawing a transparent mask 
     UIColor.black.withAlphaComponent(0.4).setFill() 
     context.fill(targetRect) 

     // Drawing bar 
     UIColor.white.setFill() 
     context.fill(someBarRect) 

     return UIGraphicsGetImageFromCurrentContext() 
    } 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { UIGraphicsEndImageContext() } 
    let context = UIGraphicsGetCurrentContext()! 

    // Drawing image 
    clippedPath.addClip() 
    makeImage()?.draw(in: targetRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
    } 
} 
+0

@马特圆角当图像的栏的颜色是相同的容器视图的背景颜色不应该被看作(在这种情况下,它的集合视图) – mrahmiao

所以好像这一缺陷有事情做与UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip(),我尝试面具在步骤1中添加图像,在步骤2中的代码是像添加角落图像此

extension UIImage { 
func thumbnails() -> UIImage? { 
    let targetRect = CGRect(x: 0, y: 0, width: 100, height: 150) 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { 
     UIGraphicsEndImageContext() 
    } 

    UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip() 

    let makedImage = self.makedImage() 

    makedImage?.draw(in : targetRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
} 


func makedImage()->UIImage?{ 
    let targetRect = CGRect(x: 0, y: 0, width: 100, height: 150) 
    let someBarRect = CGRect(x: 0, y: 100, width: 100, height: 50) 

    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, 0) 
    defer { 
     UIGraphicsEndImageContext() 
    } 
    let context = UIGraphicsGetCurrentContext()! 

    draw(in : targetRect) 


    // Drawing a transparent mask 
    UIColor.black.withAlphaComponent(0.4).setFill() 
    context.fill(targetRect) 

    // Drawing bar 
    UIColor.white.withAlphaComponent(1).setFill() 
    context.fill(someBarRect) 

    return UIGraphicsGetImageFromCurrentImageContext() 
} 
} 
+0

恢复GState()之后,酒吧将不会被剪裁,底部的两个圆角消失。 – mrahmiao

+0

看起来像'UIGraphicsGetCurrentContext.clear()'是你正在寻找的方法。我还改变了顺序或'context.fill(targetRect)',以便图像没有白色的顶角。 – wj2061

+0

它确实有效。锯齿消失了。但是,无法涂上透明色条。虽然我设置了透明色填充,但无法看到透明效果。 – mrahmiao