将图像(UIImageView)覆盖到照片中

时间:2019-07-12 04:56:21

标签: ios swift avfoundation

拍照时是否可以整合UIImageView?

我似乎找不到讨论这个话题。

我想做的是在预览上覆盖UIImageView。轻按快门时,应该使用嵌入了UIImageView的图片进行拍摄。

enter image description here

所以这就是我现在的位置。 “测试图像”部分将变为可拖动到预览视图的顶部。我想将该图像包含在按下快门按钮时要拍摄的图像中。

这可能吗?还是有更好的方法?

我正在使用AVCaptureVideoPreviewLayer,AVCaptureSession和AVCapturePhotoOutput。我只是将UIIMageView放在UIView上方,AVCaptureVideoPreviewLayer也是子视图。

编辑:已测试将UIView保存为图像,并且不包含预览层(仅返回UIView + UIImageView叠加层)。

1 个答案:

答案 0 :(得分:2)

至少有两种方法可以做到这一点。

1)从相机捕获照片并根据可拖动的图像视图位置添加叠加图像。这样可以产生最好的质量。

2)如果不需要照片质量,则可以使用另一种解决方案-在图形上下文和视频预览层中渲染可拖动的视图层:

func takePhotoWithOverlay() -> UIImage? {
    let overlayImageView // UIImageView with overlay image
    let videoPreviewLayer // AVCaptureVideoPreviewLayer

    guard let superview = overlayImageView.superview else { return nil }

    UIGraphicsBeginImageContextWithOptions(superview.bounds.size, false, 0.0)
    defer { UIGraphicsEndImageContext() }

    guard let context = UIGraphicsGetCurrentContext() else { return nil }

    videoPreviewLayer.render(in: context)
    overlayImageView.layer.render(in: context)

    return UIGraphicsGetImageFromCurrentImageContext()
}