使用 UIImage drawInRect 仍然将其呈现为上下颠倒
我在这个网站上读过一些关于使用 drawInRect 而不是 CGContext 绘制方法的帖子。它仍然把它画得颠倒吗?我认为使用 drawInRect 会将其正面朝上打印,坐标系起始于左上角?
-(void) drawImage:(UIImage*) image atX:(float) x andY:(float) y withWidth:(float) width andHeight:(float) height onContext:(CGContextRef) context{
UIGraphicsPushContext(context);
[image drawInRect:CGRectMake(x, size.height-y-height, width, height)];
UIGraphicsPopContext();
}
请注意,我正在执行 size.height-y-height
,如果我不这样做,它就不会到达我期望的位置(假设 drawInRect
使用左上角坐标系) )。它使用上面的代码呈现在正确的位置,但仍然颠倒。帮助!!!!!!
更新
感谢下面的答案,这是工作方法
-(void) drawImage:(UIImage*) image atX:(float) x andY:(float) y withWidth:(float) width andHeight:(float) height onContext:(CGContextRef) context{
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[image drawInRect:CGRectMake(x, y, width, height)];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
I have read some posts on this website about using drawInRect instead of the CGContext draw methods. It still draws it upside down? I thought using drawInRect would print it right side up with the coordinate system originating at the top left?
-(void) drawImage:(UIImage*) image atX:(float) x andY:(float) y withWidth:(float) width andHeight:(float) height onContext:(CGContextRef) context{
UIGraphicsPushContext(context);
[image drawInRect:CGRectMake(x, size.height-y-height, width, height)];
UIGraphicsPopContext();
}
Notice I'm doing size.height-y-height
and if I don't do that it doesn't go where I expect (assuming drawInRect
using a topleft coordinate system). It renders in the correct spot with the above code but still upside down. HELP!!!!!!
UPDATE
Thanks to the answer below this is the working method
-(void) drawImage:(UIImage*) image atX:(float) x andY:(float) y withWidth:(float) width andHeight:(float) height onContext:(CGContextRef) context{
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[image drawInRect:CGRectMake(x, y, width, height)];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)