How do you get UIImage from AVCaptureVideoPreviewLayer?

I already tried this CGImage (or UIImage) solution from CALayer

However, I get nothing.

As in the question, I'm trying to get the UIImage from the camera preview level. I know that I can either capture a still image or use the outputbuffer outputs, but my quality video for the session is set to a photo, so either of these two uploads is slow and will give me a large image.

So, as I thought, this could be due to getting the image directly from the preview layer, since it has exactly the size that I need, and the operations are already done on it. I just don't know how to make this layer draw in my context so that I can get it as a UIImage.

Perhaps another solution would be to use OpenGL to get this layer directly as a texture?

Any help would be appreciated, thanks.

+4
source share
1 answer

Apple quote from this Technical Q & A :

A: Starting with iOS 7, the UIView class provides the -drawViewHierarchyInRect: afterScreenUpdates: method, which allows you to visualize a snapshot of the entire hierarchy of views as visible on the screen in a raster context. On iOS 6 and earlier, how to capture drawing content depends on the basic drawing technique. This new method -drawViewHierarchyInRect: afterScreenUpdates: allows you to capture the contents of the receiver view and its subzones into the image regardless of the drawing methods (for example, UIKit, Quartz, OpenGL ES, SpriteKit, AV Foundation, etc.) in which rendered

In my experience with AVFoundation, this is not the case, if you use this method on the view on which the preview layer is placed, you will only get the content of the view without the image of the preview layer. Using -snapshotViewAfterScreenUpdates: will return the UIView that hosts the special layer. If you try to make an image from this view, you will not see anything.
The only solution I know is AVCaptureVideoDataOutput and AVCaptureStillImageOutput . Everyone has their own limit. The first one cannot work at the same time as receiving AVCaptureMovieFileOutput , the latter creates shutter noises.

+3
source

All Articles