核心映像:合并两个CIImage Swift

时间:2019-03-19 05:45:47

标签: ios swift core-image cifilter ciimage

所以我有2个要合并在一起的CIImage,所以每个alpha的alpha值为0.5。我该怎么办?

我厌倦了以下代码,但生成的图像尺寸不正确,并且两个图像未正确结合...请帮助!

if let image = CIImage(contentsOf: imageURL) {
    let randomFilter = CIFilter(name: "CIRandomGenerator")
    let noiseImage = randomFilter!.outputImage!.cropped(to: (CGRect(x: CGFloat(Int.random(in: 1..<1000)), y: CGFloat(Int.random(in: 1..<1000)), width: image.extent.width, height: image.extent.height)))
    let compoimg = noiseImage.composited(over: image) //Misaligned image

} 

misaligned image

1 个答案:

答案 0 :(得分:0)

问题出在随机噪声发生器上,因为它的本质是随机噪声是从无限噪声图中裁剪出来的……正确的代码可以补偿这种翻译:

if let image = CIImage(contentsOf: imageURL) {
    let randomFilter = CIFilter(name: "CIRandomGenerator")
    let randX = CGFloat(Int.random(in: 0..<1000))
    let randY = CGFloat(Int.random(in: 0..<1000))
    let noiseImage = randomFilter!.outputImage!.cropped(to: (CGRect(x: randX, y: randY, width: image.extent.width, height: image.extent.height)))
    let tt = noiseImage.transformed(by: CGAffineTransform.init(translationX: -randX, y: -randY))
    let compoimg = tt.composited(over: image) //Correctly allied image
}