拍照时,bluView变成透明的吗?

问题描述

我有一个具有模糊效果的矩形,当我拍照时,模糊变成透明的。我的previewView中有ViewController。为什么会发生这种情况?

func photoOutput(_ output: AVCapturePhotoOutput,didFinishProcessingPhoto photo: AVCapturePhoto,error: Error?) {
        
        if let imageData = photo.fileDataRepresentation() {
        if let cameraImage = UIImage(data: imageData) {
            
            
            // Create the image context to draw in
            UIGraphicsBeginImageContextWithOptions(previewView.bounds.size,false,UIScreen.main.scale)

            // Get that context
            let context = UIGraphicsGetCurrentContext()

            // Draw the image view in the context
            previewView.layer.render(in: context!)
            
            // Then grab the "screenshot" of the context
            let image = UIGraphicsGetImageFromCurrentImageContext()
            
            //combine new image and camera view
            let newImage2 = composite(image: cameraImage,overlay: image!,scaleOverlay: true)
            
            //show captured image
            captureImageView2.frame = CGRect(x: 0,y: 0,width: self.view.frame.size.width,height: self.view.frame.size.height)
            captureImageView2.image = newImage2
            self.view.addSubview(captureImageView2)


            //end the context
            UIGraphicsEndImageContext()
        }
    }
}



//combine imageview with uiview
func composite(image:UIImage,overlay:(UIImage),scaleOverlay: Bool = false)->UIImage?{
        UIGraphicsBeginImageContext(image.size)
        var rect = CGRect(x: 0,width: image.size.width,height: image.size.height)
        image.draw(in: rect)
        if scaleOverlay == false {
            rect = CGRect(x: 0,width: overlay.size.width,height: overlay.size.height)
        }
        overlay.draw(in: rect)
        return UIGraphicsGetImageFromCurrentImageContext()
}

//This code is in my PreviewView `UIView`
private func createLayer(in rect: CGRect) {
let darkBlur = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: darkBlur)

blurView.frame = rect
blurLayer.append(blurView)

insertSubview(blurView,at: 1)

}

解决方法

Ofc变得透明,在当前上下文中绘制并清除图层...

  • 创建容器
  • 为图像ImageVC创建视图
  • 创建视图以模糊BlurVC
  • BlurVC中的设置模糊
  • 将BlurVC添加到容器
  • 将ImageVC添加到容器
  • 在ImageVC中将图像绘制到上下文

添加结果容器进行预览

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...