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 7937649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:34:26+00:00 2026-06-03T22:34:26+00:00

I am customizing my drawRect: method, which serves to draw a NSImage if it

  • 0

I am customizing my drawRect: method, which serves to draw a NSImage if it has been “loaded” (loading taking a few seconds worth of time because I’m grabbing it from a WebView), and putting off drawing the image till later if the image has not yet been loaded.

- (void)drawRect:(NSRect)dirtyRect
{
    NSImage *imageToDraw = [self cachedImage];
    if (imageToDraw != nil) {
        [imageToDraw drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
    } else {
        //I need help here
        [self setNeedsDisplay:YES];
    }
}

My question is how to do the latter. [self cachedImage] returns nil if the image is unavailable, but anytime within the next few seconds it may become available and at that time I want to draw it because the custom view is already on screen.

My initial instinct was to try calling [self setNeedsDisplay:YES]; if the image wasn’t available, in hopes that it would tell Cocoa to call drawRect again the next time around (and again and again and again until the image is drawn), but that doesn’t work.

Any pointers as to where I can go from here?


EDIT:

I am very much aware of the delegate methods for WebView that fire when the loadRequest has been completely processed. Using these, however, will be very difficult due to the structure of the rest of the application, but I think I will try to somehow use them now given the current answers. (also note that my drawRect: method is relatively light weight, there being nothing except the code I already have above.)

I currently have about 10+ custom views each with custom data asking the same WebView to generate images for each of them. At the same time, I am grabbing the image from an NSCache (using an identifier corresponding to each custom view) and creating it if it doesn’t exist or needs to be updated, and returning nil if it is not yet available. Hence, it’s not as easy as calling [view setNeedsDisplay:YES] from - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame or another method.

  • 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-03T22:34:28+00:00Added an answer on June 3, 2026 at 10:34 pm

    My initial instinct was to try calling [self setNeedsDisplay:YES]; if the image wasn’t available, in hopes that it would tell Cocoa to call drawRect again the next time around (and again and again and again until the image is drawn)

    This would be incredibly inefficient, even if it worked.

    anytime within the next few seconds it may become available and at that time I want to draw it

    So, when that happens, call [view setNeedsDisplay:YES].

    If you have no means of directly determining when the image becomes available, you’ll have to poll. Set up a repeating NSTimer with an interval of something reasonable — say 0.25 second or so. (This is also pretty inefficient, but at least it’s running only 4 times per second instead of 60 or worse. It’s a tradeoff between two factors: how much CPU and battery power you want to use, and how long the delay is between the time the image becomes available and the time you show it.)

    my drawRect: method is relatively light weight, there being nothing except the code I already have above.

    Even if you do nothing at all in -drawRect:, Cocoa still needs to do a lot of work behind the scenes — it needs to manage dirty rects, clear the appropriate area of the window’s backing store, flush it to the screen, etc. None of that is free.

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

Sidebar

Related Questions

We're customizing a CMS that has a few hundred tables in it. We are
I'm customizing a color picker's default showing colors which will be used as background
I am interested in customizing the authentication method for a Joomla website. There is
I am working on customizing a closed source client application. It has a tree
I am customizing my table cells. I have this code (simplified) which is giving
Background: I am customizing an existing ASP .NET / C# application. It has it's
I'm customizing the product page in the view.phtml file, The changes which I'm doing
I'm customizing a UIPickerView's rows, so I'm implementing the viewForRow method in it's delegate
I've been customizing the Jquery UI auto-complete to ensure that users submit only data
I have been playing with some of Apple's example code for customizing UITableViewCells. 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.