【问题标题】:How to improve the quality of CGImageRef taken from on-screen layer?如何提高从屏幕图层中获取的 CGImageRef 的质量?
【发布时间】:2012-03-14 11:21:33
【问题描述】:

使用下面的代码,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: 中绘制的图层与屏幕大小不同。 我想将屏幕切成小块,然后对其应用动画。

原始层:

绘制层:

【问题讨论】:

    标签: ios core-graphics calayer


    【解决方案1】:

    为什么首先要打开图像上下文?直接在层上绘制 ctx 作为第二个方法参数传递的上下文。

    还需要将图层的contentsScale属性设置为[UIScreen mainScreen].scale

    要使用正确的设备比例(在您的示例中不需要)绘制图像,请使用:

    void UIGraphicsBeginImageContextWithOptions(
       CGSize size,
       BOOL opaque,
       CGFloat scale // pass 0 to use screen scale
    );
    

    PS:截至目前,我真的看不出你想用你的代码完成什么!

    【讨论】:

    • 新图层的比例正确,我需要将屏幕分成几个小图层。
    • 好吧,那我已经给你答案了。使用UIGraphicsBeginImageContextWithOptions。你的方法仍然没有任何意义。为什么需要一个单独的层来分割屏幕?
    • 是的,你是对的!直接[self.view.layer renderInContext:ctx] 可以完成创建、裁剪、绘制任务,但质量仍然很差,有什么想法吗?我需要几个子层的原因是,每个子层都会向下翻转以暴露它下面的层。
    猜你喜欢
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 2020-08-01
    • 2021-03-31
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多