CALayer的反别名对角线边缘 [英] Anti-alias diagonal edges of CALayer
问题描述
当我使用CATransform3DRotate设置CALayer的transform属性时,图层被正确旋转。但是,该层的边缘是锯齿状的,没有消除锯齿。我已经阅读了几篇关于这个主题的帖子:
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:
我是试图通过在我的对角线层设置以下属性来结合他们的建议
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;
我还覆盖了drawLayer:inContext:对角层的方法以确保消除锯齿:
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);
}
我缺少什么?
推荐答案
我刚刚对此图片发了一篇文章。也许它会有所帮助。
I just did a post on this for images. Maybe it will help.
我找到了一些帮助和设置边框的技巧没有按照我的想法做到。你可以做的是设置一些设置来帮助插值,栅格化和rasterisationScale。
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:
UIImage * img =[UIImage imageWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle ] resourcePath ] stringByAppendingPathComponent:@"AliasImage.png" ] ] ];
CGRect imageRect = CGRectMake( 0 , 0 , img.size.width + 4 , img.size.height + 4 );
UIGraphicsBeginImageContext( imageRect.size );
[img drawInRect:CGRectMake( imageRect.origin.x + 2 , imageRect.origin.y + 2 , imageRect.size.width - 4 , imageRect.size.height - 4 ) ];
CGContextSetInterpolationQuality( UIGraphicsGetCurrentContext() , kCGInterpolationHigh );
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[aliasImage setImage:img ];
aliasImage.transform = CGAffineTransformScale(aliasImage.transform , 0.45 , 0.45 );
aliasImage.layer.shouldRasterize = YES;
aliasImage.layer.rasterizationScale = 0.45;
aliasImage.layer.edgeAntialiasingMask = kCALayerLeftEdge | kCALayerRightEdge | kCALayerBottomEdge | kCALayerTopEdge;
aliasImage.clipsToBounds = NO;
aliasImage.layer.masksToBounds = NO;
我发布了一些例子这里
这篇关于CALayer的反别名对角线边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!