Iphone 使用UIImage drawInRect仍会将其倒置

Iphone 使用UIImage drawInRect仍会将其倒置,iphone,uiimage,cgcontext,Iphone,Uiimage,Cgcontext,我在这个网站上读过一些关于使用drawInRect而不是CGContext draw方法的帖子。还是把它倒过来?我认为使用drawInRect可以将其打印到右上角,坐标系从左上角开始 -(void) drawImage:(UIImage*) image atX:(float) x andY:(float) y withWidth:(float) width andHeight:(float) height onContext:(CGContextRef) context{ UIGraphicsP

我在这个网站上读过一些关于使用drawInRect而不是CGContext draw方法的帖子。还是把它倒过来?我认为使用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();
}

我在UIImage上使用StretcableImageWithCapWidth方法,因此我必须使用drawInRect来利用此功能。请先尝试翻译上下文,然后使用drawInRect,如果它适合您的话。
    UIGraphicsBeginImageContext(imageViewSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();

// Draw the image in the upper left corner (0,0) with its actual size
CGContextDrawImage(imageContext, imageViewRect, oldImage.CGImage);

//  As it draws the image from lower right corner, 
//  following code will flip it up side down vertically.


CGContextTranslateCTM(imageContext, 0.0, 0.0);
CGContextScaleCTM(imageContext, 1.0, -1.0);

CGContextDrawImage(imageContext, imageViewRect, oldImage.CGImage);