如何解决针对高分辨率iOS设备的drawRect中冗长的图形的性能问题?

 收藏

My app has a lengthy graph to be displayed where the X-Axis is marked with years ranging from 400 to 2100. The graph is drawn in a UIView and added as sub view to a UIScrollView. The axis is drawn with year markers using CGContextRef in drawRect as mentioned in this link.

由于轴长,图形(UIView)框架的宽度设置为10000。在中等分辨率的设备(如iPad 2)中绘制此图形时没有问题。但是在高分辨率的设备(如iPad Air)中打开此图形时,背景图不存在问题绘制(变得透明),并且在滚动时,由于重新绘制了内容而不删除先前绘制的内容,整个图形变得凌乱。

即使我在“ setFrame”委托中也调用了以下方法,但没有用。

[self setNeedsLayout];
[self setNeedsUpdateInRect:self.frame]; 

谁能建议以有效的方式在drawRect中绘制图形来处理高分辨率设备的性能?

以下是我在drawRect委托中的代码:

CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];

CGPoint startPoint = CGPointMake(graphEndPadding, graphBaselineY);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);

//Create line from starting point to end point
CGPoint endPoint = CGPointMake(graphLengthPixels+graphEndPadding, graphBaselineY);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);

//Create hashmark point system
CGFloat currX = startPoint.x;
CGFloat yVal = startPoint.y + graphHashMarkLength;

//Draw hashmarks
int count = 0;
while(currX <= endPoint.x) {
    //Draw hashmark
    CGContextMoveToPoint(context, currX, graphBaselineY);
    CGContextAddLineToPoint(context, currX, yVal);

    //Move to next hashmark
    currX += pixelYearIncrement;
    count++;
}

//Draw x-axis
CGContextSetLineWidth(context, 1);
CGContextSetLineCap(context, cgLineCapSquare);
CGContextSetRGBStrokeColor(context, 0.7765, 0.6118, 0.4275, 1.0);
CGContextStrokePath(context);
回复