首页 > 解决方案 > 当我拍照时,我的 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, y: 0, width: image.size.width, height: image.size.height)
        image.draw(in: rect)
        if scaleOverlay == false {
            rect = CGRect(x: 0, y: 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)

}

标签: swiftavfoundationuivisualeffectview

解决方案


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

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

添加结果容器以进行预览


推荐阅读