简体   繁体   中英

Draw only the good rect

I have a large image managed with UIScrollView and CATiledLayer (like the Large Image Downsizing iOS sample code ). I had a drawing view ( UIView overrided with drawing methods) on it in order to draw lines and rectangles. I'm trying to find a way to redraw only the visible rect when I zoom in on the image in order to improve the performances.

I found the setNeedsDisplayInRect() method and I'm using it like this :

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

But in my drawRect() method, for now, I redraw all the lines and rectangles. How can I know which visible lines I have to redraw ?

You will need something like this, depending on how you draw the image

- (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
            }
        }
    }
}

You determine where the lines are, and then stroke them using nsbezierpath/cgpath , right ?

Probably, most of the time will be spent in stroking algorithm, not determining . Just set path's clip region to drawRect's dirtyRect argument. Dummy strokes should cost nothing then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM