简体   繁体   中英

Freezing OS X Screen in Cocoa

I have two methods that modify the display of the screen. Is there a way to freeze the screen until the second method is called?

// I want to freeze the screen here
updateDisplay1(); // Change in display doesn't take effect until screen is unfrozen
updateDisplay2();
// Unfreeze screen here

In Excel VBA this would be something like screenUpdating = FALSE. Is there a similar function in Cocoa?

One possible way is to take screenshot:

// Take screenshot of the screen
// Display the screenshot image in front of the screen
updateDisplay1(); // Change is not visible
updateDisplay2();
// Remove screenshot image

But I'm afraid this is slow and takes up a lot of memory. Is there a more efficient method?

What screen are you talking about?

Standard Cocoa behaviour is:

  1. you update the properties of the various views you are interested in;
  2. each of those will make a call to its own setNeedsDisplayInRect . As a result of those calls, a redraw will be scheduled on the run loop;
  3. once you've finished doing all of your updates, the run loop will be able to move onto its next item;
  4. eventually it'll reach the redraw it scheduled and will make appropriate drawRect calls to appropriate views.

So all view changes that you make in response to an action are automatically atomic. You have to go significantly out of your own way to create a partial update.

taking screenshots with CGWindowListCreateImage takes a few milliseconds and uses roughly any memory. it's usually done directly in the graphic card.

give it a try and measure before doing any performance assumptions:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM