【问题标题】:Draw only the good rect只画好的矩形
【发布时间】:2012-12-26 11:51:37
【问题描述】:

我有一个用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() 方法中,现在我重画了所有的线条和矩形。我怎么知道我必须重绘哪些可见线?

【问题讨论】:

    标签: objective-c ios image drawing


    【解决方案1】:

    你需要这样的东西,这取决于你如何绘制图像

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-01-04
        • 1970-01-01
        • 2023-03-28
        • 2012-04-03
        • 2016-12-03
        • 2017-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多