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

2

6

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.

Crossed answered 7/4, 2013 at 14:39 Comment(3)
I also had this issue a while back. The issue was that the camera preview layer (as well as some custom rendering layers) are using OpenGL. The method you are using does not work with GL backed layers as I recall. I will ask around and see if this is in fact true.Vedanta
@SteveMcFarlin have you find a way to solve the issue, I'm facing same problem #41239754Accuse
Did you find solutions ?Accuse
S
2

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)
Seasoning answered 2/1, 2014 at 16:57 Comment(2)
I am confused because this solution doesn't seem to work but it's been chosen. I've tried all kinds of variations of this method but no luck. It's still all black. Did this really work?Vaasa
@Vaasa I've tried and it doesn't work, did you found a solution ?Accuse
M
0

I too am currently looking for a solution to this problem!

I am currently out at the moment so I can't test what I have found, but take a look at these links:

Screenshots-A Legal Way To Get Screenshots seems like its on the right track - here is the Example Project (and here is the initial post)

When I manage to get it to work I will definitely update this answer!

Marlin answered 10/4, 2013 at 6:5 Comment(1)
I've too seen those posts, but I couldn't manage to implement it :/Crossed

© 2022 - 2024 — McMap. All rights reserved.