V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
ahmiao
V2EX  ›  iDev

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

  •  
  •   ahmiao · 2016-12-08 17:41:19 +08:00 · 2943 次点击
    这是一个创建于 2693 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码

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

    参见图片

    截图

    2 条回复    2016-12-09 08:04:09 +08:00
    imhui
        1
    imhui  
       2016-12-08 22:57:49 +08:00
    看起来像是出现了锯齿问题。
    试一下改成这样:
    UIGraphicsBeginImageContextWithOptions(targetRect.size, false, [UIScreen mainScreen].scale)
    ahmiao
        2
    ahmiao  
    OP
       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.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1115 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:42 · PVG 06:42 · LAX 15:42 · JFK 18:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.