如何将两个 CIImages 与 alpha 结合起来?
How can I combine two CIImages with alpha?
在我的项目中,我想将两个 CIImages 组合在一起。
为此,我使用“CISourceInCompositing”
if let currentFilter = CIFilter(name: "CISourceInCompositing") {
let bgImage = inputImage
var filterImage = resizeCIImage(image: filterImage, newWSize: bgImage.extent.size)
filterImage = setOpacity(image: filterImage, alpha: opacity)
currentFilter.setValue(filterImage, forKey: kCIInputImageKey)
currentFilter.setValue(bgImage, forKey: kCIInputBackgroundImageKey)
guard let outputImage = currentFilter.outputImage else {
return CIImage()
}
return outputImage
}
但是我想改变“kCIInputImageKey”的不透明度
所以我用这个方法:
func setOpacity (image : CIImage, alpha : Double) ->CIImage {
guard let overlayFilter: CIFilter = CIFilter(name: "CIColorMatrix") else { fatalError() }
let overlayRgba: [CGFloat] = [0, 0, 0, alpha]
let alphaVector: CIVector = CIVector(values: overlayRgba, count: 4)
overlayFilter.setValue(image, forKey: kCIInputImageKey)
overlayFilter.setValue(alphaVector, forKey: "inputAVector")
return overlayFilter.outputImage!
}
但是我得到的图像和我想要的不一样:
你有什么解决办法吗?
谢谢
您应该使用 CISourceOverCompositing
而不是 CISourceInCompositing
。
CISourceInCompositing
的定义:
Uses the background image to define what to leave in the input image, effectively cropping the input image.
CISourceOverCompositing
的定义
Places the input image over the input background image.
在此处查看其他 CoreImage 复合操作的信息和示例输出:https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CISourceOverCompositing
在我的项目中,我想将两个 CIImages 组合在一起。 为此,我使用“CISourceInCompositing”
if let currentFilter = CIFilter(name: "CISourceInCompositing") {
let bgImage = inputImage
var filterImage = resizeCIImage(image: filterImage, newWSize: bgImage.extent.size)
filterImage = setOpacity(image: filterImage, alpha: opacity)
currentFilter.setValue(filterImage, forKey: kCIInputImageKey)
currentFilter.setValue(bgImage, forKey: kCIInputBackgroundImageKey)
guard let outputImage = currentFilter.outputImage else {
return CIImage()
}
return outputImage
}
但是我想改变“kCIInputImageKey”的不透明度 所以我用这个方法:
func setOpacity (image : CIImage, alpha : Double) ->CIImage {
guard let overlayFilter: CIFilter = CIFilter(name: "CIColorMatrix") else { fatalError() }
let overlayRgba: [CGFloat] = [0, 0, 0, alpha]
let alphaVector: CIVector = CIVector(values: overlayRgba, count: 4)
overlayFilter.setValue(image, forKey: kCIInputImageKey)
overlayFilter.setValue(alphaVector, forKey: "inputAVector")
return overlayFilter.outputImage!
}
但是我得到的图像和我想要的不一样:
你有什么解决办法吗? 谢谢
您应该使用 CISourceOverCompositing
而不是 CISourceInCompositing
。
CISourceInCompositing
的定义:
Uses the background image to define what to leave in the input image, effectively cropping the input image.
CISourceOverCompositing
Places the input image over the input background image.
在此处查看其他 CoreImage 复合操作的信息和示例输出:https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CISourceOverCompositing