如何解决拍照时,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,width: image.size.width,height: image.size.height)
image.draw(in: rect)
if scaleOverlay == false {
rect = CGRect(x: 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中将图像绘制到上下文
添加结果容器进行预览
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。