丸い角のサムネイル画像と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()
}
}
画像のバーの色は、コンテナビューの背景色と同じである場合に丸みを帯びた角が見えないべき@matt(この場合には、それがコレクションビューの) – mrahmiao