2012-10-07 4 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

관련 문제