AVCaptureVideoPreviewLayer add overlays and capture photo in iOS

时间:2019-04-08 13:04:58

标签: ios swift avfoundation metal core-image

The initial idea was to start a camera stream via AVCaptureSession, find faces in that raw CMSampleBuffer and then add some images as layers on AVCaptureVideoPreviewLayer and then take a screenshot.

After completing that, found out later that the UIGraphicsGetImageFromCurrentImageContext won't work with AVCaptureVideoPreviewLayer, so taking screenshot would not solve my purpose here.

So I used Metal and MTKView instead to perform some live rendering and the results are good with the combination of CoreImage Filters and Metal. I already know how to detect faces and alter that part of the face using inbuilt CoreImage filters but I can't find a suitable method to add an image on to another image.

How can I blend two images with respect to positioning in the background image? I have CIImage to work with.

1 个答案:

答案 0 :(得分:2)

您可以将叠加层加载到CIImage中,然后使用transformed(by matrix: CGAffineTransform)将其移动到面部位置,最后使用composited(over dest: CIImage)将叠加层混合到{视频缓冲区。 您可能需要进行一些工作才能在不同的坐标空间之间进行转换。

还有许多更复杂的合成过滤器可用。在CIImage类别中查看filters