创建一个带有抗锯齿的圆圈或磁盘,用于视网膜显示 [英] Create a circle or a disk with antialiasing for retina display
问题描述
我使用 CoreGraphic
中的 CGContextFillEllipseInRect
创建了一个圆圈。
I have creating a circle using CGContextFillEllipseInRect
from CoreGraphic
.
我正在使用这个圆圈(实际上是一个磁盘)来替换<$ c的 thumbImage
$ C> UISlider 。默认情况下会应用抗锯齿功能。
I'm using this circle (which is actually a disk) to replace the thumbImage
of an UISlider
. The antialiasing is applied by default.
但我的iPhone 6上的结果显然不太好。我可以清楚地看到像素,而不是关闭抗锯齿,但比普通UISlider的像素更多。
But the result on my iPhone 6 is clearly not good. I can clearly see the pixels ,not as much as with the antialiasing off, but way more than the pixels of a normal UISlider.
也许我做错了什么。所以我的问题是,有没有办法以编程方式获得与UISlider默认使用的相同的优秀磁盘?
Maybe I'm doing something wrong. So my question is, is there a way to get programmatically the same nice disk than the one used by default for an UISlider?
编辑:
以下是我创建磁盘的方法:
Here is how I create the disk:
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
,如在实况提到心理状态:
The problem is that you are creating a non-retina graphics context when using UIGraphicsBeginImageContext
, as mentioned in the documentation:
此功能等同于调用与不透明参数的 UIGraphicsBeginImageContextWithOptions
功能设置为否
,比例因子 1.0
。
This function is equivalent to calling the
UIGraphicsBeginImageContextWithOptions
function with the opaque parameter set toNO
and a scale factor of1.0
.
相反,您应该使用 UIGraphicsBeginImageContextWithOptions
来创建图像上下文。如果你想要一个支持透明度的图像(与你现在隐含的相同),你可以继续传递 NO
作为opaque参数。
Instead you should be using UIGraphicsBeginImageContextWithOptions
to create your image context. You can keep passing NO
for the opaque parameter if you want an image that supports transparency (same as what you are implicitly doing now).
在大多数情况下,您可以为比例因子传递 0.0
。这会将比例因子设置为设备主屏幕的比例因子。同样,如文档中所述:
In most cases you can pass 0.0
for the scale factor. This sets the scale factor to that of the device's main screen. Again, as mentioned in the documentation:
如果指定值
0.0
,比例因子设置为设备主屏幕的比例因子。
If you specify a value of
0.0
, the scale factor is set to the scale factor of the device’s main screen.
因此,简而言之,您应该像这样创建图像上下文:
So, in short, you should create your image context like this:
UIGraphicsBeginImageContextWithOptions(rectForDisk.size, false, 0.0) // false for Swift
这篇关于创建一个带有抗锯齿的圆圈或磁盘,用于视网膜显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!