2011-08-31 23 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); 
相關問題