problem w/ grabscreen and NSOperationQueue

I’m having lots of fun using of on iphone and ipad. I’ve noticed an issue with grabscreen that I would love some help on.

I am writing an application that lets you create some audiovisual stuff, and then save a screen image using of’s grabScreen function. It all works great. I realized, though, that the saving process takes some time on the iPad (I’m actually saving multiple images plus an audio file that could be large), so I should show a UIActivityIndicatorView that spins and maybe gray everything out too. I found some example code that uses an NSOperationQueue to start and stop the activity indicator animation, and that works, and the images and sound save during it. BUT… the images generated by grabScreen come out all black. I know the save operation itself is working fine since the audio saves correctly.

So… is grabScreen not “thread safe,” or is there some other problem with using it along with NSOperationQueue? Any ideas for workarounds?

Thanks!

Hey,

I guess it"s because ofGrabScreen() uses OpenGL calls and OpenGL calls must be done on the main thread!

Regards,
Gestalt

aha! So it turns out all I had to do was swap. Instead of putting the save method (including grabscreen) into a separate thread, I left it on the main thread, and put the activity indicator animation on the separate thread.

Here’s some code in case it helps anybody:

  
-(void)viewDidLoad {  
	myApp = (testApp*)ofGetAppPtr();  
	  
	// wait view to block UI during saving   
	waitView = [[UIView alloc] initWithFrame:[[[UIApplication sharedApplication] keyWindow] frame]];  
	[waitView setBackgroundColor:[UIColor grayColor]];  
	[waitView setAlpha:0.5];  
	[[[UIApplication sharedApplication] keyWindow] addSubview:waitView];  
	[waitView setHidden:YES];  
	  
	indicatorView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];  
	[indicatorView setCenter:[[[UIApplication sharedApplication] keyWindow] center]];  
	[self.view addSubview: indicatorView];  
	  
	opQueue = [[NSOperationQueue alloc] init];  
	  
}  
- (IBAction)startSaving: (id)sender  
{  
	NSInvocationOperation *request = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(showSavingAnimation:) object:self];  
	[opQueue addOperation:request];  
	[request release];  
	  
	myApp->saveDrawing();  
	[waitView setHidden:YES];  
	[indicatorView stopAnimating];  
}  
- (void)showSavingAnimation:(id)object {  
	[indicatorView startAnimating];  
	[waitView setHidden:NO];  
}