2010-07-23 33 views
0

美好的一天;CATiledLayer或CALayer繪圖UIImage不能正常工作

我想手動(不使用子圖層)在CATiledLayer內繪製圖像,但它不像應用定義的解決方案那樣運行。我承諾,當你調用'CGContextDrawImage'時,你必須縮放和翻譯以便翻轉它,但是我不能爲了我的生活,讓它工作。

我有一個名爲

- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image 

方法,在

- (void)drawInContext:(CGContextRef)context 

打過幾次電話從渲染所有進入CATileLayer圖像。

下使無圖像:

- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image { 
    CGRect rect = CGRectMake(x, y, image.size.width, image.size.height); 
    CGContextSaveGState(context); 

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

    CGContextRestoreGState(context); 

下產生圖像,但呈現不正確:

- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image { 
    CGRect rect = CGRectMake(x, y, image.size.width, image.size.height); 
    CGContextDrawImage(context, rect, image.CGImage); 

像明智的這樣:

- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image { 
    CGRect rect = CGRectMake(x, y, image.size.width, image.size.height); 

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

我也有嘗試:

UIGraphicsBeginImageContext 
UIGraphicsEndImageContext 

UIGraphicsPushContext 
UIGraphicsPopContext 

所有不起作用。我錯過了一些基本的東西,我懷疑我的方法來保存上下文不起作用。

回答

0

好吧我想我知道這裏發生了什麼;

看起來圖像實際上是渲染,但是在屏幕上渲染。翻轉不是相對於層的起源發生的,而是超層的起源。

我看到的問題是我有一個大圖層和一個大網格的子圖層。我認爲,在子層內翻轉,將是相對於它自己的合作,但事實並非如此。