我有一个很大的UIScrollView,其中包含一个CATiledLayer,我用它在drawRect:中绘制一个大得多的磁贴,如下所示:
- (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、CGContextScaleCTM和CGContextRestoreGState调用,但图像颠倒时,这是有效的。调用到位后,根本不会绘制图像。我可以使用平铺drawInRect:但这会反转绘制行,这会使大图变得混乱。
我在翻译中做错了什么?
编辑:按照建议将保存/恢复和转换移出循环,但它仍然没有绘制任何内容。
发布于 2012-01-09 03:03:30
设置正确的转换以垂直翻转内容是出了名的困难。看不到任何东西的可能原因,是因为变换将图像移到了矩形之外。我以前让它工作过,但不记得我是怎么做到的。现在,我在CATiledLayer上设置了"geometryFlipped = YES“,这将为我做翻转。
顺便说一下,为什么不将CATiledLayer的"tileSize“设置为您的tiles的大小,那么您就不需要这种for循环tiles映射的东西了。每个磁贴都会调用一次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);
}发布于 2012-01-08 22:23:13
首先,将所有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);
}https://stackoverflow.com/questions/8778224
复制相似问题