0

I have live camera in my app (in scrollView) and for transition I need to take screenshot of camera preview and get that image, but

 drawHierarchy(in rect: CGRect,afterScreenUpdates afterUpdates: Bool) 

method doesn't help. Is there a way to achieve this without OpenGL. Please help, cause it's already several days I am searching the solution.

 UIGraphicsBeginImageContextWithOptions((self.getRectOfSceneInScrollView(index: self.selectedSceneIndex!).size), false, 0);
self.scrollView.drawHierarchy(in: CGRect.init(--some rect--), afterScreenUpdates: true)

let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!;

UIGraphicsEndImageContext()

UPDATE: self.scrollView.layer.render(in: UIGraphicsGetCurrentContext()!) doesn't help too

kawake123
  • 53
  • 8

2 Answers2

1

Apple has some restrictions on capturing the AVCaptureVideoPreviewLayer from our custom camera view by using UIGraphicsGetImageFromCurrentImageContext. Even UIView Snapchat method will not work if we have camera view.

But we do have a workaround to get the effect. Get the image buffer for each camera frame and convert it to UIImage. Add the image to our screenshot manually. It's not that simple and AFAIK this is the best workaround. More info can be found in SO Answer from Is it possible to render AVCaptureVideoPreviewLayer in a graphics context?

Bluewings
  • 3,438
  • 3
  • 18
  • 31
  • Thank You @Bluewings for answer, it could be helpful, but the problem is that I want that screenshot not only during or after capturing, I need it after a button click for example, for transitions and animations, that's the problem – kawake123 Jul 06 '17 at 07:10
  • If you want to get an image and use it for transitions, the only way i can think of is taking pictures from live camera and use it for transitions and animations – Bluewings Jul 06 '17 at 07:12
-2
func takeScreenshot(view: UIView) -> UIImageView {
        UIGraphicsBeginImageContext(view.frame.size)
        view.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

        return UIImageView(image: image)
    }
BHAVIK
  • 890
  • 7
  • 35