2

我知道我必须在绘图时翻译坐标系。问题是当我使用:

CGContextTranslateCTM(_context, 0.0, _size.height);
CGContextScaleCTM(_context, 1.0, -1.0);

我的图像的矩形被翻转,图像是“非翻转”的,如果我不使用上面的图像,我的图像被翻转,矩形是非翻转的。

这是我的代码:

CGRectMake(60, 100, image.size.width, image.size.height);
CGContextSaveGState(_context);
UIGraphicsBeginImageContext(image.size);
CGContextClearRect(_context, placeholderRect);
CGContextDrawImage(_context, placeholderRect, image.CGImage);
UIGraphicsEndImageContext();
CGContextRestoreGState(_context);

当图像被翻转时,我如何保持我的矩形(非翻转)?

谢谢

4

2 回答 2

3

而不是使用CGContextDrawImage使用[image drawAtPoint:CGPointMake(0,0)];

于 2012-09-04T13:34:05.227 回答
1

诀窍可能在于这个 UIImage 方法

+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation

其中方向是其中之一:)

typedef enum {
   UIImageOrientationUp,
   UIImageOrientationDown,   // 180 deg rotation
   UIImageOrientationLeft,   // 90 deg CCW
   UIImageOrientationRight,   // 90 deg CW
   UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
   UIImageOrientationDownMirrored,  // horizontal flip
   UIImageOrientationLeftMirrored,  // vertical flip
   UIImageOrientationRightMirrored, // vertical flip
} UIImageOrientation;

编辑:更新

我刚刚在覆盖 drawrect 的 UIView 子类中做了这个简单的代码示例,它工作得很好。它不使用 UIImage 方法,但工作方式相同。

UIImage *image = [UIImage imageNamed:@"image.png"];

CGContextRef _context = UIGraphicsGetCurrentContext();

CGContextSaveGState(_context);

CGContextDrawImage(_context, CGRectMake(100, 20, image.size.width, image.size.height), image.CGImage);

CGContextScaleCTM(_context, -1.0f, 1.0f);

CGContextDrawImage(_context, CGRectMake(-300, 20, image.size.width, image.size.height), image.CGImage);

CGContextRestoreGState(_context);
于 2012-06-05T11:41:44.707 回答