保存用户在AVCaptureVideoPreviewLayer中看到的内容 [英] Save what user see in AVCaptureVideoPreviewLayer

查看:75
本文介绍了保存用户在AVCaptureVideoPreviewLayer中看到的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义相机开发应用程序,用户可以在其中添加滤镜或标签(例如在TextCamera应用程序中)并分享社交Feed.但是我发现了我的第一个问题.

I'm trying to develop an app with custom camera where the user can add filters or sticker (like in TextCamera app) and share in social feed. But I found my first problem.

我使用AVCaptureVideoPreviewLayer向用户显示预览,拍摄照片并将其传递给UiImageView中的另一个视图控制器,但是第二张图片比第一张大.

I show the preview to the user with AVCaptureVideoPreviewLayer, take the photo and pass it to another view controller in a UiImageView but the second picture is bigger than first one.

我尝试使用此功能调整图片大小:

I tried to resize the picture with this function:

    func resize(image: UIImage) -> UIImage {
       let size = image.size
       let newWidth = CGFloat(size.width)
       let newHeight = CGFloat(size.height - blackBottomTab.bounds.size.height)

       let newSize = CGSizeMake(newWidth,newHeight)
       let rect = CGRectMake(0, 0, newSize.width, newSize.height)

       UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
       image.drawInRect(rect)
       let newImage = UIGraphicsGetImageFromCurrentImageContext()
       UIGraphicsEndImageContext()

       return newImage
}

在此功能中,我从图像高度中减去黑色视图的高度(在按钮下方).但是我得到的结果却有所不同(请参阅所附照片).

In this function I subtract the height of the black view (under the button) from the image height. But the result that I have is different (see the photo attached).

这是我的预览,按钮下方有黑色视图

这是比预览的照片大的照片

我还尝试在第二个View Controller的Storyboard Image View中使用Aspect Fit,但结果是相同的.

I also tried to use Aspect Fit in Storyboard Image View of the second View Controller but the result is the same.

我的错误在哪里?谢谢所有对我有帮助的人!

Where is my error? Thank you to everyone that help me!

推荐答案

我必须解决一个类似的问题.正如问题所指出的那样,似乎没有一种简单的方法可以检测视频预览的大小.

I had to solve a similar problem. As the question notes, there does not appear to be an easy way to detect the size of the video preview.

https://stackoverflow.com/a/57996039/10449843 答案的结尾处提示了我的解决方案.a>详细说明了如何拍摄快照并创建带有标签的组合快照.

My solution is hinted at at the end of the answer to https://stackoverflow.com/a/57996039/10449843 which explains in detail how I take the snapshot and create the combined snapshot with the sticker.

这里是对该提示的详细说明.

Here is an elaboration of that hint.

当我使用AVCapturePhotoCaptureDelegate拍摄快照时,我还使用AVCaptureVideoDataOutputSampleBufferDelegate对缓冲区进行一次采样,以便在首次显示预览时检测快照的比例.

While I use AVCapturePhotoCaptureDelegate to take the snapshot, I also use AVCaptureVideoDataOutputSampleBufferDelegate to sample the buffer once when the preview is first shown to detect the proportions of the snapshot.

            // The preview layer is displayed with aspect fill
            let previewLayer = AVCaptureVideoPreviewLayer(session: session)
            previewLayer.videoGravity = .resizeAspect
            previewLayer.frame = self.cameraView.bounds

检测预览的大小:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        // Only need to do this once
        guard self.sampleVideoSize == true else {
            return
        }

        guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
            return
        }

        // Sample the size of the preview layer and then store it
        DispatchQueue.main.async {

            let width = CGFloat(CVPixelBufferGetWidth(pixelBuffer))
            let height = CGFloat(CVPixelBufferGetHeight(pixelBuffer))

            // Store the video size in a local variable
            // We don't care which side is which, we just need the
            // picture ratio to decide how to align it on different
            // screens.
            self.videoSize = CGSize.init(width: width, height: height)

            // Now we can set up filters and stickers etc
            ...

            // And we don't need to sample the size again
            self.sampleVideoSize = false
        }
        return
    }

这篇关于保存用户在AVCaptureVideoPreviewLayer中看到的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆