Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:44:00+00:00 2026-06-12T11:44:00+00:00

The following is my self-defined view’s drawRect, it draw a chess board, the board

  • 0

The following is my self-defined view’s drawRect, it draw a chess board, the board has 19*19 lines, and character a-t at top and bottom sides, 1-19 at left and right sides, also 9 dots:

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

Running on iPad3, this method takes more than 1 second. Is there any suggestion on how to optimize it?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:44:00+00:00Added an answer on June 12, 2026 at 11:44 am

    I assume this follows on from your earlier question about poor rotation performance.

    Creating and drawing (and scaling) images and fonts is expensive. Using the time profiler in instruments will highlight the most expensive areas of your code, but those are the most likely candidates.

    You should consider decomposing your view into UIImageView and UILabel instances, which will cache their backing stores and not need to be redrawn, just repositioned. However, you shouldn’t do anything until you’ve profiled your code and found the performance bottlenecks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My project has the following scenario: [Preview View Controller] --(Modal)--> Navigation Controller --(Top)--> [Photo
Removing annotations from my map view in the following way: if ([[self.mapView annotations] count]
I have the following method on a backbone view defined in coffeescript: saveObservation: =>
I have the following Form defined class MyForm(ModelForm): def __init__(self, readOnly=False, *args, **kwargs): super(MyForm,self).__init__(*args,**kwrds)
I am doing this with the following: [[self navigationController] setNavigationBarHidden:YES animated:YES]; and also I
I am not a fan of the following construction if (self = [super init])
The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
I have the following code: def query(self,query): lock = QMutexLocker(self.mutex) reply = self.conn.query(query) if
The following code: with open(J:\\python\\.data) as data: self.data=pickle.load(data) generated the following error: File J:\python\code.py,
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.