2012-10-07 70 views
0

以下是我自定義視圖的drawRect,它畫一個棋盤,棋盤上有19 * 19行,字符在上下兩邊,左邊和右邊1-19,還有9個點:如何優化drawRect?

- (void)drawRect:(CGRect)rect 
{ 
    // Get the drawing context 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSaveGState(context); 

    // Draw the board background 
    UIImage *bk = [UIImage imageNamed:@"board_bg_011.png"]; 

    CGColorRef shadowColor = CreateDeviceRGBColor(0.5, 0.5, 0.5, 1); 
    CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 10, shadowColor); 
    [bk drawInRect:CGRectMake(TEXT_AREA_OFFSET, TEXT_AREA_OFFSET, self.bounds.size.width - TEXT_AREA_OFFSET * 2, self.bounds.size.height - TEXT_AREA_OFFSET * 2)]; 

    CGColorRelease(shadowColor); 
    CGContextRestoreGState(context); 

    // Draw board edage square 
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:tableRect]; 
    path.lineWidth = 2.0; 
    [path stroke]; 

    // Draw board lines 
    UIBezierPath *line = [UIBezierPath bezierPath]; 
    line.lineWidth = 1.0; 
    for (int i = 1; i <= 17; i ++) { 
     float x = tableRect.origin.x + i * qiziSize; 
     [line moveToPoint:CGPointMake(x, tableRect.origin.y)]; 
     [line addLineToPoint:CGPointMake(x, tableRect.origin.y + tableRect.size.height)]; 
     [line stroke]; 

     float y = tableRect.origin.y + i * qiziSize; 
     [line moveToPoint:CGPointMake(tableRect.origin.x, y)]; 
     [line addLineToPoint:CGPointMake(tableRect.origin.x + tableRect.size.width, y)]; 
     [line stroke]; 
    } 

    // Draw 9 dots 
    CGContextSaveGState(context); 

    CGRect dotRect = CGRectMake(0, 0, 6, 6); 
    CGContextTranslateCTM(context, tableRect.origin.x + qiziSize * 3 - 3, tableRect.origin.y + qiziSize * 3 - 3); 
    UIBezierPath *dot = [UIBezierPath bezierPathWithOvalInRect:dotRect]; 
    [dot fill]; 

    CGContextTranslateCTM(context, qiziSize * 6, 0); 
    [dot fill]; 

    CGContextTranslateCTM(context, qiziSize * 6, 0); 
    [dot fill]; 

    CGContextTranslateCTM(context, - qiziSize * 12, qiziSize * 6); 
    [dot fill]; 

    CGContextTranslateCTM(context, qiziSize * 6, 0); 
    [dot fill]; 

    CGContextTranslateCTM(context, qiziSize * 6, 0); 
    [dot fill]; 

    CGContextTranslateCTM(context, - qiziSize * 12, qiziSize * 6); 
    [dot fill]; 

    CGContextTranslateCTM(context, qiziSize * 6, 0); 
    [dot fill]; 

    CGContextTranslateCTM(context, qiziSize * 6, 0); 
    [dot fill]; 

    CGContextRestoreGState(context); 

    CGContextSaveGState(context); 

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:12]; 
    int fontHeight = [@"1" sizeWithFont:font].height; 
    UIColor *textColor = [UIColor grayColor]; 
    [textColor setFill]; 
    [textColor setStroke]; 

    for (int i = 1; i <= 19; i ++) { 
     // Top text 
     NSString *str = [NSString stringWithFormat:@"%c", ((i < 9)?i:(i+1)) + 'A' - 1]; 
     CGRect textRect = CGRectMake(tableRect.origin.x + qiziSize * (i - 1) - qiziSize/2, 0, qiziSize, TEXT_AREA_OFFSET); 
     [str drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter]; 

     // Bottom text 
     textRect.origin.y = self.bounds.size.height - TEXT_AREA_OFFSET + 2; 
     [str drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter]; 

     // Left text 
     str = [NSString stringWithFormat:@"%i", i]; 
     textRect = CGRectMake(0, tableRect.origin.y + qiziSize * (i - 1) - fontHeight/2, TEXT_AREA_OFFSET - 2, fontHeight); 
     [str drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight]; 

     // Right text 
     textRect.origin.x = self.bounds.size.width - TEXT_AREA_OFFSET + 2; 
     [str drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentLeft]; 
    } 

    CGContextRestoreGState(context); 
} 

在iPad3上運行此方法需要1秒以上。有沒有關於如何優化它的建議?

在此先感謝。

回答

2

我認爲這是從你之前關於旋轉性能差的問題開始的。

創建和繪製(和縮放)圖像和字體很昂貴。在儀器中使用時間分析器將突出顯示代碼中最昂貴的區域,但這些是最有可能的候選。

您應該考慮將視圖分解爲UIImageView和UILabel實例,這些實例將緩存其後備存儲,而不需要重新繪製,只需重新定位即可。但是,在你分析代碼並發現性能瓶頸之前,你不應該做任何事情

+0

隨着儀器工具,我發現[UIImage drawInRect]吃大部分CPU時間。那是畫棋盤的背景,我用了一個大的png文件。試圖註釋drawingInRect,性能顯着提高。謝謝。 – TieDad