讨论CGContextDrawImage

这个函数绘制图片,但坐标系统原点在左上角,y方向向下的(坐标系A),但在Quartz中坐标系原点在左下角,y方向向上的(坐标系B)。图片绘制也是颠倒的。

要达到预想的效果必须变换坐标系,代码如下:

void drawImage(CGContextRef context, CGImageRef image , CGRect rect){

   CGContextSaveGState(context);

 

        CGContextTranslateCTM(context, rect.origin.x, rect.origin.y);//4

        CGContextTranslateCTM(context, 0, rect.size.height);//3

        CGContextScaleCTM(context, 1.0, -1.0);//2

        CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);//1

        CGContextDrawImage(context, rect, image);

 

        CGContextRestoreGState(context);

}

A到B变换 通过1->2->3->4步骤实现的,这样好理解些

通常我会用UIImage drawInRect实现想要的功能。

posted @ 2011-06-12 17:56  Delon  阅读(4179)  评论(1编辑  收藏  举报