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

标签 ios swift cgcontext quartz-2d

我正在尝试在 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()
  }
}

最佳答案

所以这个陷阱似乎与 UIBezierPath(roundedRect: targetRect, cornerRadius: 8).addClip() 有关,我尝试在步骤 1 中为图像添加蒙版,添加角到步骤2中的image,代码是这样的

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()
 }
}

关于ios - 在 iOS 上绘制圆角图像时遇到奇怪的锯齿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41050939/

相关文章:

ios - 使用 CGContextShowTextAtPoint 在图像上书写后的像素化文本

iphone - 如何将 UIView 渲染成两部分

ios - 将文本写入 CGContext 的奇怪错误

iphone - 如何随着时间的推移增加间隔?

ios - 如何在应用程序中显示 apns token ?在 swift 4

ios - 有没有一种快速扫描 iOS 7 应用程序以获取 iOS 8 API 的方法?

ios - 我的 firebase int 返回 nil

objective-c - 无法获得模糊效果

iOS iBeacon 读数不一致

ios - 将数据从 tableView 传递到 UIViewController swift 3