2011-08-31 37 views
0

我有与该代码的自定义视图:iPhone - 图像绘制倒置放入一个CGGraphic上下文(自定义的UIView)

- (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); 
} 

的图像是一个箭头,pointiing到顶部。

但是,当绘制时,它是上下颠倒,指向底部。

是什么导致了这个问题?
我该如何纠正,自然无副作用?

+1

顺便说一句,应该没有理由保留然后释放从imageNamed获得的UIImage。 – picciano

+0

是的,我知道,这是为了测试目的 – Oliver

+0

看看[这个]问题(http://stackoverflow.com/questions/506622/cgcontextdrawimage-draws-image-upside-down-when-passed-uiimage-cgimage)的问题。 –

回答

3

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

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

或者你可以尝试在你的绘制方法(Source)使用此代码:

CGContextTranslateCTM(context, 0, image.size.height); 
CGContextScaleCTM(context, 1.0, -1.0);