ios - 为什么当我结合 UIImagview 时我的模糊 View 变得透明?

标签 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
}

最佳答案

终于开始工作了:

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/

相关文章:

ios - 将代码保留在 <# #> 括号中

iphone - 在 xcode 目标属性中看不到代码签名权利

Xcode 10.3 不适用于 MacOS Big Sur 11.0.1(非测试版)

ios - GeometryReader使文本高度错误

android - 如何检查 FirebaseAnimatedList 是否在 flutter 中返回数据

ios - 使用 AVCaptureSession 和 AVAssetWriter 在翻转相机的同时进行无缝录音

swift - 尝试将 Apple "translated"的 KeychainItemWrapper 用于 Swift

ios - CFBundleShortVersionString 必须是大多数三个非负整数的句点分隔列表

objective-c - macOS Swift : How to properly add application as Login Item

ios - 当 DatePicker 隐藏单元格时移动 ScrollView