与UIImagview结合使用时,为什么模糊视图变得透明?

时间:2018-09-11 14:24:40

标签: ios swift xcode avfoundation

我具有结合UIImageView的模糊效果。我遇到的问题是,当我拍照时,模糊效果变得透明,并且在渲染之前看起来并不一样。为什么会这样?

@IBAction func pictureButton(_ sender: Any) {
    let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
    stillImageOutput.capturePhoto(with: settings, delegate: self)


}

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

    guard let imageData = photo.fileDataRepresentation()
        else { return }

    let image = UIImage(data: imageData)

    capturedImage.image = image

    UIGraphicsBeginImageContextWithOptions(self.capturedImage.bounds.size, false, 0.0)
    self.capturedImage.layer.render(in: UIGraphicsGetCurrentContext()!)
    self.previewView.blurEffectView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    self.capturedImage.image = newImage
}

1 个答案:

答案 0 :(得分:0)

让它终于可以工作:

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

    guard let imageData = photo.fileDataRepresentation()
        else { return }

    let image = UIImage(data: imageData)
    capturedImage.image = image


    let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
    let image2 = renderer.image { ctx in
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    }
    self.capturedImage.image = image2
}