4

我在这个网站上阅读了一些关于使用 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();
}
4

1 回答 1

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);
于 2010-12-21T04:40:12.047 回答