描述一下,这两天用到了,CGContextDrawImage 这个函数,在重写我的drawrect函数的时候,我要在某个地方画一个图。然后画嘛~刚开始也没注意,后来发现图画倒了。按照正常程序员的思路,画布的坐标是从屏幕的左上开始!!为了不误导别人,我把上面那句划掉。
然后找原因,在官方文档中找到了原因!
Cocoa和Quartz用的是基础坐标系!左下 是我们所谓的原点!具体参见官方文档。下面参考链接2
废话不说,直接上图!
怎么办!
下面说解决办法!
1) get the current graphics context;
2) translate and flip the coordinate system;
3) transform the drawing rectangle;
具体代码
CGContextSaveGState(context);//入栈
CGContextTranslateCTM(context, rect.origin.x, rect.origin.y);
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
CGContextDrawImage(context, rect, image);
CGContextRestoreGState(context);//出栈
这样图就调过来了!
参考链接:http://blog.ddg.com/?p=10A;https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/Transforms.html