繁体   English   中英

创建带有抗锯齿的圆圈或磁盘以显示视网膜

[英]Create a circle or a disk with antialiasing for retina display

我已经使用CoreGraphic CGContextFillEllipseInRect创建了一个圆。

我正在使用这个圆圈(实际上是一个磁盘)替换thumbImageUISlider 默认情况下应用抗锯齿。

但是我的iPhone 6上的结果显然不好。 我可以清楚地看到像素,不像使用抗锯齿功能那样多,但是比普通UISlider的像素要多。

也许我做错了。 所以我的问题是,有没有办法以编程方式获得与默认用于UISlider的磁盘相同的磁盘?

编辑:

这是我创建磁盘的方法:

class func drawDisk(rgbValue:UInt32, rectForDisk: CGRect = CGRectMake(0.0, 0.0, 1.0, 1.0)) -> UIImage {
    let color = uicolorFromHex(rgbValue)
    UIGraphicsBeginImageContext(rectForDisk.size)
    let context = UIGraphicsGetCurrentContext()

    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillEllipseInRect(context, rectForDisk)

    let rectForCircle = CGRectMake(0.5, 0.5, rectForDisk.size.width - 1, rectForDisk.size.height - 1)
    CGContextSetLineWidth(context, 1.0)
    CGContextSetStrokeColorWithColor(context,
            UIColor.blackColor().CGColor)
    CGContextAddEllipseInRect(context, rectForCircle)
    CGContextStrokePath(context)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image
}

问题是,如文档中所述,您在使用UIGraphicsBeginImageContext时正在创建非视网膜图形上下文:

此函数等效于将opaque参数设置为NO并将比例因子设置为1.0来调用UIGraphicsBeginImageContextWithOptions函数。

相反,您应该使用UIGraphicsBeginImageContextWithOptions创建图像上下文。 如果要支持透明性的图像(与您现在隐式执行的操作相同),可以继续为opaque参数传递NO

在大多数情况下,您可以将0.0作为比例因子。 这会将比例因子设置为设备主屏幕的比例因子。 再次,如文档中所述:

如果将值指定为0.0 ,则比例因子将设置为设备主屏幕的比例因子。


因此,简而言之,您应该像这样创建图像上下文:

UIGraphicsBeginImageContextWithOptions(rectForDisk.size, false, 0.0) // false for Swift

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM