我已阅读本网站上有关使用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
使用了一个topleft坐标系)。它使用上面的代码呈现在正确的位置,但仍然是颠倒的。 HELP !!!!!!
更新
由于下面的答案,这是工作方法
-(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();
}
答案 0 :(得分:1)
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);