Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a UIView to redraw immediately, instead of during next run loop

Tags:

iphone

uiview

I've created a UIImagePicker / camera view, with a toolbar and custom button for taking a snapshot. I can't really change to using the default way because of the custom button, and I'm drawing on top of the view.

When you hit the button, I want to take a screenshot using UIGetScreenImage(); however, the toolbar is showing up in the image, even if I hide it first:

//hide the toolbar
self.toolbar.hidden = YES; 

// capture the screen pixels
CGImageRef screenCap = UIGetScreenImage();

I'm pretty sure this is because even though the toolbar is hidden, it gets redrawn once the function returns and we enter the next run loop - after UIGetScreenImage is called.

I tried making the following addition, but it didn't help:

//hide the toolbar
self.toolbar.hidden = YES; 
[self.toolbar drawRect:CGRectMake(0, 0, 320, 52)];

// capture the screen pixels
CGImageRef screenCap = UIGetScreenImage();

I also tried using setNeedsDisplay, but that doesn't work either because once again the draw happens after the current function returns.

Any suggestions? Thanks!

like image 964
Justin Kent Avatar asked May 13 '10 22:05

Justin Kent


2 Answers

The update is done in the run loop.

Simply add :

self.toolbar.hidden = YES;

[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];

CGImageRef screenCap = UIGetScreenImage();

like image 137
Antoine Rosset Avatar answered Oct 11 '22 21:10

Antoine Rosset


Try hiding the UI elements in a separate selector, and using the -performSelectorOnMainThread:withObject:waitUntilDone: method, using YES for the waitUntilDone argument. Then follow that up with another selector for screen capture.

like image 29
Alex Reynolds Avatar answered Oct 11 '22 23:10

Alex Reynolds