CALayer 的抗锯齿对角边缘
当我使用 CATransform3DRotate 设置 CALayer 的变换属性时,图层会正确旋转。然而,图层的边缘是锯齿状的并且没有抗锯齿。我读过关于这个主题的几篇文章:
iPhone - 将透视应用到 CALayer 时出现锯齿状边缘
我尝试通过在对角线图层上设置以下属性来采纳他们的建议,
CALayer* layer = myView.layer;
layer.shadowOpacity = 0.01;
layer.edgeAntialiasingMask = kCALayerTopEdge | kCALayerBottomEdge | kCALayerRightEdge | kCALayerLeftEdge;
layer.borderWidth = 1.0;
layer.borderColor = [UIColor clearColor].CGColor;
layer.backgroundColor = [UIColor greenColor].CGColor;
我还重写了对角线图层的 drawLayer:inContext: 方法以确保抗锯齿:
-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillRect(context, self.bounds);
}
我缺少什么?
When I set the transform property of my CALayer with a CATransform3DRotate, the layer is properly rotated. However, the edges of the layer are jagged and not anti-aliased. I've read a couple posts on the subject:
iPhone: CALayer + rotate in 3D + antialias?
iPhone - Jagged Edges when applying perspective to CALayer
I've tried to incorporate their advice by setting the following properties on my diagonal layer
CALayer* layer = myView.layer;
layer.shadowOpacity = 0.01;
layer.edgeAntialiasingMask = kCALayerTopEdge | kCALayerBottomEdge | kCALayerRightEdge | kCALayerLeftEdge;
layer.borderWidth = 1.0;
layer.borderColor = [UIColor clearColor].CGColor;
layer.backgroundColor = [UIColor greenColor].CGColor;
I've also overridden the drawLayer:inContext: method of my diagonal layer to ensure anti-aliasing:
-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillRect(context, self.bounds);
}
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 iOS7 开始,现在终于可以在每层基础上实现这一点,并且没有任何丑陋的黑客(例如透明像素)或解决方法(例如光栅化):
layer.allowsEdgeAntialiasing = YES;
有趣的是,官方并未涵盖这一点CALayer有文档说明,但绝对是一个公共API。请参阅 iOS7 API 差异 和 Peter Steinberger 关于 iOS7 中的隐藏宝石和解决方法。
As of iOS7, this is now finally possible on a per-layer basis and without any ugly hacks like transparent pixels or workarounds like rasterization:
layer.allowsEdgeAntialiasing = YES;
Interestingly, this is not covered by the official CALayer documentation, but is definitely a public API. See the iOS7 API diffs and Peter Steinberger's great write-up on Hidden Gems and Workarounds in iOS7.
我刚刚为此发表了一篇关于图像的文章。也许会有帮助。
我发现了一些有用的技巧,但仅设置边框并没有达到我的预期。您可以做的是设置一些设置来帮助插值、光栅化和光栅化比例。
看看这段代码中的内容是否有帮助:
我在此处发布了一些示例
I just did a post on this for images. Maybe it will help.
I found a few tricks that help and setting just a border did not do what I thought it would. What you can do is set a few settings to help with interpolation, rasterizing, and the rasterisationScale.
See if something from this code helps:
I have some examples posted here