CGContext的垂直翻转 [英] Vertical flip of CGContext

查看:480
本文介绍了CGContext的垂直翻转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView,我试图使用[CALayer renderInContext:]渲染到UIImage中。但是,我发现结果图像是垂直翻转的。由于坐标系不同,我有点期待这一点。然而,我尝试用仿射变换将上下文恢复正常 - 但它没有任何效果:

I have a UIView that I am trying to render into a UIImage using [CALayer renderInContext:]. However, I find that the resultant image is flipped vertically. I kind of expect this due to the different coordinate systems. However, I then try and flip the context back to normal with an affine transform - but it doesn't have any effect:

CGAffineTransform flipVertical = CGAffineTransformMake(
    1, 0, 0, -1, 0, imageContextHeight
);
CGContextConcatCTM(imageContext, flipVertical);
CGImageRef cgImage = CGBitmapContextCreateImage(imageContext);
UIImage* uiImage = [[UIImage imageWithCGImage:cgImage] retain];
CGImageRelease(cgImage);

我做错了什么?

推荐答案

CTM影响未来的绘图;你正在捕捉已经画出的东西。你需要在绘制之前连续转换,而不是在之后。

The CTM affects future drawing; you're capturing what you have already drawn. You need to concat that transformation before you draw, not after.

这篇关于CGContext的垂直翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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