Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a screenshot of an UIView where its subviews are camera sessions

I'm building an app where I need to take a screenshot of a view whose subviews are camera sessions (AVFoundation sessions). I've tried this code:

CGRect rect = [self.containerView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.containerView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Which effectively gets me an UIImage with the views, only that the camera sessions are black:

enter image description here

I've tried the private method UIGetScreenImage() and works perfectly, but as Apple doesn't allows this, I can't use it. I've also tried the one in Apple's docs but it's the same. I've tracked the problem to AVFoundation sessions using layers. How can I achieve this? The app has a container view with two views which are stopped camera sessions.

like image 933
pmerino Avatar asked Apr 07 '13 14:04

pmerino


1 Answers

If using iOS 7, it's fairly simple and you could do something like this from a UIViewController:

UIView *snapshotView = [self.view snapshotViewAfterScreenUpdates:YES];

You can also use this link from a widow: iOS: what's the fastest, most performant way to make a screenshot programatically?

For iOS 6 and earlier, I could only find the following Apple Technical Q&A: [How do I take a screenshot of my app that contains both UIKit and Camera elements?]

  1. Capture the contents of your camera view.
  2. Draw that captured camera content yourself into the graphics context that you are rendering your UIKit elements. (Similar to what you did in your code)
like image 170
kanso Avatar answered Oct 11 '22 06:10

kanso