iPhone - 颠倒成CGGraphic上下文的图像(自定义UIView)

时间:2011-08-31 22:56:35

标签: iphone uiview core-graphics coordinates

我有一个包含该代码的自定义视图:

- (void)drawRect:(CGRect)rect
{
    UIImage* theImage = [UIImage imageNamed:@"blue_arrow"];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextSetTextDrawingMode(context, kCGTextFill);

    CGPoint posOnScreen = self.center;
    CGContextDrawImage(context, CGRectMake(posOnScreen.x - theImage.size.width/2, 
                                           posOnScreen.y - theImage.size.height/2,                             
                                           theImage.size.width, 
                                           theImage.size.height), 
                       theImage .CGImage);
}

图像是一个箭头,指向顶部。

但是在绘制时,它是颠倒的,指向底部。

导致这个问题的原因是什么? 我怎么能纠正这一点,自然没有副作用?

1 个答案:

答案 0 :(得分:3)

这是因为Core Graphics正在使用翻转坐标系。

您可以尝试使用isFlipped属性翻转您要绘制的对象。

或者您可以尝试在绘图方法(Source)中使用此代码:

CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
相关问题