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

  • SEARCH
  • Home
  • 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 8033463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:42:21+00:00 2026-06-05T01:42:21+00:00

I have a variable sized UIView with a drawRect containing CoreGraphics custom drawing code.

  • 0

I have a variable sized UIView with a drawRect containing CoreGraphics custom drawing code. The view was originally quite small but has the potential to become very wide and is placed inside a UIScrollView to enable the user to scroll through the view.

At the moment the view is created to be as wide as it needs to be to fit the entire drawing on and the CoreGraphics code draws across the entire view in drawRect.

- (void)drawRect:(CGRect)rect {

    // Only try and draw if the frame size is greater than zero and the graph has some range
    if ((self.frame.size.width>0) && (self.frame.size.height>0) && ((maxX - minX)>0)) {

        // Get the current drawing context
        CGContextRef context = UIGraphicsGetCurrentContext();

        // Save the state of the graphics context
        CGContextSaveGState(context);

        // Set the line style
        CGContextSetRGBStrokeColor(context, 0.44, 0.58, 0.77, 1.0); 
        CGContextSetRGBFillColor(context, 0.44, 0.58, 0.77, 1.0);
        CGContextSetLineWidth(context, 3.0);
        CGContextSetLineDash(context, 0, NULL, 0);

        // Draw the graph
        [self drawGraphInContext:context];

        // Restore the graphics context
        CGContextRestoreGState(context);
    }
}

I can make this more efficient by:

  1. Doing bounding calcs with the rect parameter in the drawRect and only drawing stuff that is currently visible.
  2. Creating a smaller UIView (say 2x the width of the scrollView frame) and as the scroll position moves, redraw the image and re-centre the scrollView.

Before I go to the effort of doing either of these things, I would like to understand if it is worth the effort and whether I could run into problems later on if the UIView becomes very wide. So far performance is not an issue, but I am worried that memory might be.

Specifically, does the scrollView limit the amount of memory used by the view to the visible area? is the graphics context created the size of the entire view, the visible area or the size of the rect parameter? Are drawing operations outside the visible area simply ignored by CoreGraphics?

Any help and best practice advice would be appreciated.

  • 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-05T01:42:23+00:00Added an answer on June 5, 2026 at 1:42 am

    IMO, it all depends on the rect argument to the call:

    - (void)drawRect:(CGRect)rect
    

    and as far as I know, this will be set to the contentSize of your scroll view. Simply, the context and the memory used for it will be related to that rect value.

    You might trying doing your own “clipping” of the area you draw in by taking into account the frame of the scroll view.

    Better yet, you could have a look at a sample by Apple about tiling content in a UIScrollView. It is sample 3.

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

Sidebar

Related Questions

I have many variable-sized lists containing instances of the same class with attribute foo,
i have variable $lang which convert string to the selected language but i am
I have a variable foo that contains a time, lets say 4pm today, but
I have a parent div with a variable number of equally sized child divs
I am a beginner and I have a file having variable sized records; there
I have a variable of type size_t , and I want to print it
I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
I have a variable that has this string: <DIV><SPAN style=FONT-FAMILY: Tahoma; FONT-SIZE: 10pt>[If the
I have this array, for example (the size is variable): x = [1.111, 1.122,
I have variable @count of datatype int.I am setting values to this @count. I

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.