0

我有一个用UIScrollViewand管理的大图像CATiledLayer(如Large Image Downsizing iOS sample code)。我有一个绘图视图(UIView用绘图方法覆盖)以绘制线条和矩形。当我放大图像时,我试图找到一种仅重绘可见矩形以提高性能的方法。

我找到了这个setNeedsDisplayInRect()方法,我正在使用它:

CGRect visibleRect = CGRectApplyAffineTransform(scrollView.bounds, CGAffineTransformMakeScale(1.0 / imageScale, 1.0 / imageScale));
[self.drawingView setNeedsDisplayInRect:visibleRect];

但在我的drawRect()方法中,现在,我重画了所有的线条和矩形。我怎么知道我必须重绘哪些可见线?

4

2 回答 2

0

您确定线条的位置,然后使用 对它们进行描边nsbezierpath/cgpath,对吗?

可能大部分时间都会花在描边算法上,而不是determining. 只需将路径的剪辑区域设置为drawRect's dirtyRect参数。那时,虚拟笔画应该不花任何钱。

于 2012-12-26T12:48:15.813 回答
0

您将需要这样的东西,具体取决于您如何绘制图像

- (void)drawRect:(CGRect)rect {
    CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
    CGSize _tileSize = tiledLayer.tileSize;

    int firstCol = floorf(CGRectGetMinX(rect) / _tileSize.width);
    int lastCol = floorf((CGRectGetMaxX(rect)-1) / _tileSize.width);
    int firstRow = floorf(CGRectGetMinY(rect) / _tileSize.height);
    int lastRow = floorf((CGRectGetMaxY(rect)-1) / _tileSize.height);

    for (int row = firstRow; row <= lastRow; row++) {
        for (int col = firstCol; col <= lastCol; col++) {
            CGRect tileRect = CGRectMake(_tileSize.width * col, _tileSize.height * row, _tileSize.width, _tileSize.height);

            tileRect = CGRectIntersection(self.bounds, tileRect);

            // do your drawing
            }
        }
    }
}
于 2012-12-26T12:00:31.567 回答