[求助] 在绘制圆角图片时碰到个问题,某些颜色下会出现圆角不光滑的情形

2016-12-08 17:41:19 +08:00
 ahmiao

代码

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

参见图片

2952 次点击
所在节点    iDev
2 条回复
imhui
2016-12-08 22:57:49 +08:00
看起来像是出现了锯齿问题。
试一下改成这样:
UIGraphicsBeginImageContextWithOptions(targetRect.size, false, [UIScreen mainScreen].scale)
ahmiao
2016-12-09 08:04:09 +08:00
@imhui 嗯,是锯齿,一直没想起这个词。不过传入 [UIScreen mainScreen].scale 和 传入 0 效果是一样的,文档里写了

> scale The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device ’ s main screen.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/326242

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX