CGContextDrawImage绘制非常模糊的大图像 [英] CGContextDrawImage draws large images very blurry

查看:597
本文介绍了CGContextDrawImage绘制非常模糊的大图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CGContextDrawImage(...)制作一个可以绘制较大(例如2048 x 1537)图像的一部分的对象.它工作得很好,只是它非常模糊.我使用的是一个"drawingController",它覆盖了drawLayer:inContext:

I'm trying to make an object that can draw a portion of a large (say 2048 x 1537) image using CGContextDrawImage(…). It works just fine, except that it is extremely blurry. I'm using a "drawingController" which overrides drawLayer:inContext: like this

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    CGImageRef drawImage = CGImageRetain(imageToDraw);
    if(drawImage) {
        CGRect drawRect = CGRectMake(0, 0, 1024, 748);//Just hard coded to my window size for now.
        CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
        CGContextDrawImage(ctx, drawRect, drawImage);
    }
    CGImageRelease(drawImage);
}

这是我设置自定义图像托管视图的方式:

And this is how I set up my custom image hosting view:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"large" ofType:@"jpg"];
        CGImageRef imageRef = [self createCGImageFromFilePath:filePath];


        drawingController = [[DrawingLayerController alloc] init];
        drawingController.imageToDraw = imageRef;

        [self setWantsLayer:YES];
        imageLayer = [[CALayer alloc] init];
        imageLayer.frame = self.bounds;
        imageLayer.drawsAsynchronously = YES;
        imageLayer.delegate = drawingController;
        [self.layer addSublayer:imageLayer];

        imageLayer.contentsRect = CGRectMake(0.5, 0.265452179, 0.5, 0.486662329); //Just an example contents rect.
        imageLayer.drawsAsynchronously = YES;

        [imageLayer setNeedsDisplay];
        [imageLayer displayIfNeeded];
    }
    return self;
}

这是在预览中打开的类似缩放比例下沿源图像生成的图像的一部分,因此您可以看到图像与源图像相比有多模糊.

Here is a portion of the image produced along side the source image at a similar zoom scale opened in Preview, so you can see just how blurry the image is as compared to the source image.

如您所见,我的代码生成的图像看起来很可怕.这是怎么回事?

As you can see, the image produced by my code looks HORRIBLE. What is up with that?

此外,出于完整性考虑,我尝试对我的imageLayer使用CATiledLayer.生成的图像看起来也很糟糕.

Also, just for completeness, I have tried using a CATiledLayer for my imageLayer. The resulting image looks just as awful.

最后一点:代码需要与iOS和OSX兼容,但是我正在OSX上进行所有测试.

One final note: The code needs to be compatible with iOS AND OSX, but I am doing all my testing on OSX.

感谢@Andrea,我在CGContextDrawImage(...)上方添加了以下内容

Thanks to @Andrea, I've added the following above CGContextDrawImage(…)

CGFloat scale = MAX(1,MAX(CGImageGetWidth(drawImage)/drawRect.size.width,CGImageGetHeight(drawImage)/drawRect.size.height));

layer.contentsScale = scale;

推荐答案

我不完全了解osx,但我可以给您一些建议.

I don't know exactly for osx but I can give you some advices.

  • 按照逗号中的说明检查contentScale属性,我还写了一个答案
  • 您的contentGravity是哪个?
  • 在第一次尝试中保持简单,您真的需要绘制异步信号吗?
  • Check the contentScale property as stated in commets, I wrote also a question about it, but I never had a real answer
  • Most of time blurriness is due to unmatching pixel, this asks to the system to apply antialiasing on the image, I see that you set a contentsRect this, with the contentsGravity, probably could change proportion and scaling of the image turning it into a blur one. Here a good answer about it.
  • Which is your contentGravity?
  • Keep it simple in first try, do you really need to draw async?

希望这会有所帮助.

这篇关于CGContextDrawImage绘制非常模糊的大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆