Schrodingrrr
Schrodingrrr

Reputation: 4271

Screenshot of window is taking too long

I am taking the screenshot of the app window on user's tap action using the following code:

UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, self.view.opaque, 0.0);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];

CGImageRef imageRef = CGImageCreateWithImageInRect([UIGraphicsGetImageFromCurrentImageContext() CGImage], CGRectMake(0, 0, 640, self.view.window.layer.frame.size.height *2));

-performing a few operations on imageContext-

myImage = UIGraphicsGetImageFromCurrentImageContext();

Now, the screenshot is just fine, but the amount of time it takes to render it is excruciating. I need to present an animation as soon as the user taps, but i cannot begin the animation until the screenshot image is stored in myImage.

Is there a speedy way of doing this, like Facebook does on it's wall (when image browser is opened), or are they using some other technique?

Upvotes: 2

Views: 119

Answers (1)

Schrodingrrr
Schrodingrrr

Reputation: 4271

I managed to speed up the process a 'little' bit by distributing the tasks between the background and main threads.

I performed the following tasks on the background thread dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //code }); :

  • Begin CGImageContext
  • Create CGImageRef
  • Create screenshot using renderInContext
  • Perform the operations on the imageRef i need.
  • Pass the final UIImage to a selector, which further sets the image to the variable i want (UIImageView in my case) .
  • End and Release the graphics contexts.

In the selector in invoked at the end of the block, i set the image to the UIImageView using dispatch_async(dispatch_get_main_queue(), ^{ //code
});

Upvotes: 1

Related Questions