Anti-alias diagonal edges of CALayer

前端 未结 2 1989
轻奢々
轻奢々 2020-12-28 19:05

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

2条回答
  •  温柔的废话
    2020-12-28 19:30

    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:

        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;
    

    I have some examples posted here

提交回复
热议问题