2012-01-08 27 views
1

我有一個大UIScrollView含有CATiledLayer我用畫的更大的地磚這drawRect:這樣的:CATiledLayer,CGContextDrawImage和CGContextTranslateCTM

- (void)drawRect:(CGRect)rect { 
    int firstCol = floorf(CGRectGetMinX(rect)/tileSize); 
    int lastCol = floorf((CGRectGetMaxX(rect)-1)/tileSize); 
    int firstRow = floorf(CGRectGetMinY(rect)/tileSize); 
    int lastRow = floorf((CGRectGetMaxY(rect)-1)/tileSize); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 
    CGContextTranslateCTM(context, 0, tileSize); 
CGContextScaleCTM(context, 1.0, -1.0); 

    for(int row = firstRow; row <= lastRow; row++) { 
    for(int col = firstCol; col <= lastCol; col++) { 
      UIImage = [self getTileWithRow:row column:col]; 

      CGRect tileRect = CGRectMake((col * tileSize), 
             row * tileSize), 
             tileSize, tileSize); 

      CGContextTranslateCTM(context, 0, tileRect.size.height); 
      CGContextScaleCTM(context, 1.0, -1.0); 
      CGContextDrawImage(context, tileRect, tile.CGImage); 
     } 
    } 

    CGContextRestoreGState(context); 
} 

當我註釋掉CGContextSaveGState這工作, CGContextSaveGState,CGContextScaleCTMCGContextRestoreGState調用,但圖像顛倒。通過調用,圖像根本不會被繪製。我可以使用[tile drawInRect:],但是會反向繪製行,從而將較大的圖像擰緊。

我在做什麼錯誤的翻譯?

編輯:按照建議移動保存/恢復並轉換出循環,但它仍然沒有繪製任何東西。

回答

4

設置權轉換翻轉內容垂直異常困難。沒有看到任何東西的可能原因是因爲轉換將圖像移動到矩形之外。我之前做過這件事,但不記得我是如何做到的。現在我在CATiledLayer上設置了「geometryFlipped = YES」,它爲我做了翻轉。順便說一下,爲什麼不將CATiledLayer的「tileSize」設置爲你的瓷磚的大小,那麼你不需要這個for-loop瓷磚貼圖的東西。 drawRect被調用一次爲每個瓷磚,所以你可以這麼做:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    int col = floorf(CGRectGetMinX(rect)/tileSize); 
    int row = floorf(CGRectGetMinY(rect)/tileSize); 

    UIImage tile = [self getTileWithRow:row column:col]; 

    CGContextDrawImage(context, rect, tile.CGImage); 
} 
+0

我沒有意識到有一個geomtryFlipped屬性,這已經幫助了很多,謝謝。我還更新了tileSize以減少代碼並消除循環,並且它看起來很棒。 – JWood 2012-01-09 15:11:09

2

首先,將所有CGContextSaveGState/CGContextRestoreGState移出這些循環,因爲這些會在您的實施中增加比所需更多的工作量。

其次,將CGContextRef context = UIGraphicsGetCurrentContext();添加爲drawRect方法的第一行。

所以您的實現應該是這樣的美好:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 
    CGContextTranslateCTM(context, 0, rect.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    int firstCol = floorf(CGRectGetMinX(rect)/tileSize); 
    int lastCol = floorf((CGRectGetMaxX(rect)-1)/tileSize); 
    int firstRow = floorf(CGRectGetMinY(rect)/tileSize); 
    int lastRow = floorf((CGRectGetMaxY(rect)-1)/tileSize); 

    for(int row = firstRow; row <= lastRow; row++) 
    { 
     for(int col = firstCol; col <= lastCol; col++) 
     { 
      UIImage = [self getTileWithRow:row column:col]; 

      CGRect tileRect = CGRectMake((col * tileSize), 
             row * tileSize), 
             tileSize, tileSize); 

      CGContextDrawImage(context, tileRect, tile.CGImage); 
     } 
    } 
    CGContextRestoreGState(context); 
} 
+0

謝謝,我已經更新,包括你的建議,但它仍然沒有畫任何東西。這是否可以歸結爲它是一個CATiledLayer,並且翻譯正在將座標移動到可見矩形的外部? – JWood 2012-01-08 14:31:44