【发布时间】:2014-11-02 23:28:05
【问题描述】:
我有一个自定义的UIView,它会在覆盖中绘制一些东西
- (void)drawRect:(CGRect)rect
这很好用,并且在视网膜屏幕上提供了清晰的结果。
但是,现在我想让绘图所基于的属性可动画化。似乎只能在CALayer 上创建动画属性,所以我没有在UIView 中进行绘图,而是创建了一个自定义CALayer 子类并在内部进行绘图
- (void) drawInContext:(CGContextRef)ctx
我在此函数中使用的绘图代码与在自定义 UIView 的 drawRect 函数中使用的绘图代码几乎完全相同。
结果看起来一样 - 但是,它不是视网膜分辨率,而是像素化(大方形像素)
如果我放了
self.contentsScale = [UIScreen mainScreen].scale;
在我的drawInContext 实现开始时,我得到的不是像素化结果,而是模糊的结果(好像渲染仍然以非视网膜分辨率执行,然后放大到视网膜分辨率)。
在CALayers drawInContext 中渲染锐利视网膜路径的正确方法是什么?
这里是一些截图(蓝线是有问题的自定义绘图的一部分。黄色部分只是一张图片)
在自定义 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 中渲染时
【问题讨论】:
-
在retina显示中,需要将高和宽乘以2。
-
不,屏幕上的大小和位置是正确的
-
感谢您的帮助,但我认为您在这里没有正确理解我的问题
-
如果把
self.contentsScale = [UIScreen mainScreen].scale移到层初始化方法呢? -
你解决过这个问题吗?我有完全相同的问题,使用 shouldRasterize 和设置 rasterizationScale 没有效果。
标签: ios core-graphics calayer retina