swift - 当我拍照时,我的 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)
}
解决方案
Ofc 你变得透明,你在当前上下文中绘制并清除图层......
- 创建容器
- 为图像 ImageVC 创建视图
- 为模糊 BlurVC 创建视图
- 在 BlurVC 中设置模糊
- 将 BlurVC 添加到容器
- 将 ImageVC 添加到容器
- 在 ImageVC 中将图像绘制到上下文中
添加结果容器以进行预览
推荐阅读
- npm - 由于权限错误,无法使用 npm 安装 appium
- node.js - 通过存储客户端将文件发送到存储桶中的文件夹
- angular - 如何使管道始终工作,而不是检查更改?
- java - 如何修复“在配置的阻塞超时(60000 [ms])内没有可用的托管连接”
- python - 指定范围内的 Python bin 数据
- python-3.x - RuntimeError:减少步骤 2 后:设备端断言触发了 Jupyter Notebook 上的 Pytorch
- python - 我们可以对 json 模式的属性字段进行检查吗?
- angular - ag-grid cellRendererFramework 不能以角度工作
- node.js - 如何在谷歌云构建上升级节点 js 版本
- javascript - 如何根据不同的属性对对象数组进行排序?