只绘制好的矩形

时间:2012-12-26 11:51:38

标签: objective-c ios image drawing

我有一个使用UIScrollViewCATiledLayer管理的大图片(如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()方法中,现在,我重绘了所有的线条和矩形。我怎么知道哪些可见的线条需要重绘?

2 个答案:

答案 0 :(得分: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
            }
        }
    }
}

答案 1 :(得分:0)

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

可能大部分时间都花在抚摸算法上,而不是determining。只需将路径的剪辑区域设置为drawRect's dirtyRect参数即可。虚拟笔画应该没有任何费用。