我有这种结合了 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
}
最佳答案
终于开始工作了:
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
}
关于ios - 为什么当我结合 UIImagview 时我的模糊 View 变得透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278181/