我设法自己制作了聚焦效果,但是我发现了另一个问题。 每次我通过CIDepthBlurEffect深度渲染照片时,奇怪的垂直线都会显示如下:
它们总是出现在照片顶部的同一位置。
您知道如何解决此问题吗? 请告诉我您的想法。
编辑:我留下我遇到问题的代码。 抱歉,代码混乱。
深度照片
//getting depth data
func getdepth(with data: Data, guide: CIImage) -> CIImage?{
let processedimage = CIImage(data: data, options: [
CIImageOption.nearestSampling: false,
CIImageOption.auxiliaryDisparity: true,
//CIImageOption.auxiliaryDepth: true,
CIImageOption.applyOrientationProperty: true
])
if let depth = processedimage?.upsample(guide: guide){
return depth
}
return processedimage
}
//upsample
extension CIImage{
func upsample(guide: CIImage) -> CIImage?{
//let trans = CGAffineTransform(scaleX: self.extent.width / guide.extent.width, y: self.extent.height / guide.extent.height)
//let guider = guide.transformed(by: trans)
guard let filter = CIFilter(name: "CIEdgePreserveUpsampleFilter") else { return nil }
filter.setValue(self, forKey: "inputSmallImage")
filter.setValue(guide, forKey: kCIInputImageKey)
return filter.outputImage
}
}
//main image
guard let image = CIImage(data: data, options:
[CIImageOption.applyOrientationProperty: true]
) else { return }
//input variable: main: CIImage, depth: CIImage, matte: CIImage?
let transform = CGAffineTransform(
scaleX: main.extent.width / combined.extent.width,
y: main.extent.height / combined.extent.height
)
let depthscaled = depth.transformed(by: transform)
var applyingdepth: CIFilter!
if let mat = matte{
if #available(iOS 12.0, *) {
applyingdepth = CIFilter(name: "CIDepthBlurEffect", parameters: [
kCIInputImageKey: main,
kCIInputDisparityImageKey: depthscaled,
kCIInputMatteImageKey: mat
])!
}
}else {
applyingdepth = CIFilter(name: "CIDepthBlurEffect", parameters: [
kCIInputImageKey: main,
kCIInputDisparityImageKey: depthscaled
])!
}
applyingdepth.setValue(1.4, forKey: "inputAperture")
return applying depth.output // this is "portraitimage"
//outside of this block
guard let portrait = context.createCGImage(portraitimage, from: portraitimage.extent) else { return }
UIImageWriteToSavedPhotosAlbum(UIImage(cgImage: portrait), nil, nil, nil)