我正嘗試使用圓角創建縮略圖圖像,並在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()
}
}
最後我有鋒利圓角就像那些在下面的快照圖像。
任何建議,以消除圓角的鋸齒?
=======溶液======
感謝@ 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()
}
}
@馬特圓角當圖像的欄的顏色是相同的容器視圖的背景顏色不應該被看作(在這種情況下,它的集合視圖) – mrahmiao