简体   繁体   中英

iOS screenshot live camera preview

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

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?

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)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM