iOS drawing in CALayers drawInContext is either pixelated or blurry on retina(CALayers中的iOS绘图在视网膜上显示为像素化或模糊)
问题描述
我有一个自定义UIView,它在覆盖的
- (void)drawRect:(CGRect)rect
该功能运行良好,在视网膜屏幕上显示清晰的效果。
但是,现在我想将绘图所基于的属性设置为动画。创建可设置动画的属性似乎只能在CALayer上进行,因此我不是在UIView中进行绘制,而是创建一个自定义的CALayer子类并在
- (void) drawInContext:(CGContextRef)ctx
我在此函数中使用的绘制代码与在自定义UIView的drawRect函数中使用的绘制代码几乎相同。
结果看起来是一样的-但是,它不是视网膜分辨率,而是像素化的(大正方形像素)
如果我将
self.contentsScale = [UIScreen mainScreen].scale;
到我的drawInContext实现的开始,然后我得到的不是像素化的结果,而是一个模糊的结果(就好像渲染仍然以非视网膜分辨率执行,然后被提升到视网膜分辨率)。
在CALayersdrawInContext中渲染锐利视网膜路径的正确方法是什么?
以下是一些屏幕截图(蓝线是所讨论的自定义绘图的一部分。黄色部分只是一张图片)
在自定义UIView的内部绘制drawRect:
在自定义CALayer内绘制drawInContext:
在自定义CALayer的drawInContext内部绘制,首先设置self.contentScale:
为完整起见,以下是绘图代码的精简版本:
//if drawing inside custom UIView sublcass:
- (void)drawRect:(CGRect)rect
{
CGContextRef currenctContext = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextSetLineWidth(currenctContext, _lineWidth);
CGContextSetLineJoin(currenctContext,kCGLineJoinRound);
CGContextMoveToPoint(currenctContext,x1, y1);
CGContextAddLineToPoint(currenctContext,x2, y2);
CGContextStrokePath(currenctContext);
}
//if drawing inside custom CALayer subclass:
- (void) drawInContext:(CGContextRef)ctx {
{
//self.contentsScale = [UIScreen mainScreen].scale;
CGContextRef currenctContext = ctx;
CGContextSetStrokeColorWithColor(currenctContext, [UIColor blackColor].CGColor);
CGContextSetLineWidth(currenctContext, _lineWidth);
CGContextSetLineJoin(currenctContext,kCGLineJoinRound);
CGContextMoveToPoint(currenctContext,x1, y1);
CGContextAddLineToPoint(currenctContext,x2, y2);
CGContextStrokePath(currenctContext);
}
重申我想要实现的目标:我希望实现与UIView方法相同的清晰视网膜渲染,但在CALayer
推荐答案
问题很可能是Content Scale;请注意,如果通过覆盖其layerClass函数将其分配给自定义视图,层的内容比例可能会被重置。可能还有其他一些情况也会发生这种情况。为安全起见,请仅在将层添加到视图后设置内容比例。
layer.contentsScale = UIScreen.mainScreen().scale
或者,在SWIFT 4中:
layer.contentsScale = UIScreen.main.scale
这篇关于CALayers中的iOS绘图在视网膜上显示为像素化或模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CALayers中的iOS绘图在视网膜上显示为像素化或模糊
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
