2016-12-08 112 views
0

我正嘗試使用圓角創建縮略圖圖像,並在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

回答

1

所以好像這一缺陷有事情做與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