ios - 如何提高从屏幕图层获取的CGImageRef的质量?

标签 ios core-graphics calayer

使用下面的代码,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/

相关文章:

iphone - 使用 @ 作为 NSDictionary 字符串中键的一部分

cocoa-touch - 如何一一绘制布局字形 - 核心文本

iphone - Objective-C CALayer UIView 真实旋转

objective-c - 将委托(delegate)分配给 CALayer 的子层会抛出 EXC_BAD_ACCESS?

ios - 获取游戏请求ID/获取Facebook游戏请求通知内容

ios - 试图理解宏的操作顺序

ios - 标签从上到下有额外的空间吗?

iphone - 如何使用 iPhone SDK 清除圆角矩形之外的角?

ios - 使用CoreGraphics在iOS中创建圆形渐变进度栏

cocoa - NSView的CALayer可以作为Other View的子层吗?