使用下面的代码,CGContextDrawImage()绘制的图层的质量低于原始显示的图层。图像中的某些行出现了锯齿。
这是我的代码
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSaveGState(ctx);
UIGraphicsBeginImageContext(layer.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(c, YES);
CGContextSetAllowsAntialiasing(c, YES);
CGContextSetInterpolationQuality(c, kCGInterpolationHigh);
[self.view.layer renderInContext:c];
CGImageRef image = CGBitmapContextCreateImage(c);
UIGraphicsEndImageContext();
CGImageRef flipImage = CGImageCreateWithImageInRect(image, layer.bounds);
CGContextTranslateCTM(ctx, 0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0f, -1.0f);
CGContextSetShouldAntialias(ctx, YES);
CGContextSetAllowsAntialiasing(ctx, YES);
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextDrawImage(ctx, layer.bounds, flipImage);
CGContextRestoreGState(ctx);
LogMessage(@"drawLayer", 1, @"draw layer content");
}
更新1
用
drawLayer:
绘制的图层与屏幕的大小不同。我想将屏幕切成小块,然后对其应用动画。
原始层:
绘制层:
最佳答案
为什么首先要打开图像上下文?直接绘制在作为第二个方法参数传递的ctx
上下文上下文上。
此外,您需要将图层的contentsScale
属性设置为[UIScreen mainScreen].scale
。
要以正确的设备比例(示例中不需要)绘制图像,请使用以下命令:
void UIGraphicsBeginImageContextWithOptions(
CGSize size,
BOOL opaque,
CGFloat scale // pass 0 to use screen scale
);
PS:到目前为止,我真的看不到您要用代码完成什么!
关于ios - 如何提高从屏幕图层获取的CGImageRef的质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700951/