当传递UIImage.CGImage时,CGContextDrawImage绘制图像上下颠倒 [英] CGContextDrawImage draws image upside down when passed UIImage.CGImage
问题描述
有没有人知道为什么 CGContextDrawImage
会把我的图片上下颠倒?我从我的应用程序加载图像:
UIImage * image = [UIImage imageNamed:@testImage.png];
然后只要求核心图形绘制到我的上下文:
CGContextDrawImage(context,CGRectMake(0,0,145,15),image.CGImage);
它呈现在正确的位置和尺寸,但图像是颠倒的。
<$>
CGContextDrawImage(context,CGRectMake(0,0,145,15),image.CGImage);
使用
[image drawInRect:CGRectMake(0,0,145,15)];
在开始/结束中间 CGcontext
方法。
这会将图像以正确的方向绘制到当前图像上下文中 - 我确定这与 UIImage
持有方向的知识,而 CGContextDrawImage
方法获取底层原始图像数据,而不理解方向。
请注意,在许多方面你会遇到这个问题,但一个具体的例子是处理地址簿用户图像。
Does anyone know why CGContextDrawImage
would be drawing my image upside down? I am loading an image in from my application:
UIImage *image = [UIImage imageNamed:@"testImage.png"];
And then simply asking core graphics to draw it to my context:
CGContextDrawImage(context, CGRectMake(0, 0, 145, 15), image.CGImage);
It renders in the right place, and dimensions, but the image is upside down. I must be missing something really obvious here?
Instead of
CGContextDrawImage(context, CGRectMake(0, 0, 145, 15), image.CGImage);
Use
[image drawInRect:CGRectMake(0, 0, 145, 15)];
In the middle of your begin/end CGcontext
methods.
This will draw the image with the correct orientation into your current image context - I'm pretty sure this has something to do with the UIImage
holding onto knowledge of the orientation while the CGContextDrawImage
method gets the underlying raw image data with no understanding of orientation.
Note that you'll run into this problem in many areas, but one specific example is dealing with address book user images.
这篇关于当传递UIImage.CGImage时,CGContextDrawImage绘制图像上下颠倒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!