如何解决如何使用MetalPetal遮罩图像并透明输出
我有一个不透明的图像和一个不透明的蒙版图像。使用MetalPetal
及其MTIBlendWithMaskFilter
,我可以创建一个输出图像,该图像可以正确遮罩输入图像,但是不透明,具有黑色背景。
我希望输出图像具有Alpha通道,例如代替黑色像素而具有透明像素。
func mt_blend(image: UIImage,with mask: UIImage) -> UIImage {
let ciMaskImage = CIImage(cgImage: mask.cgImage!)
let mtiMaskImage = MTIImage(ciImage: ciMaskImage,isOpaque: true)
let mtiMask = MTIMask(content: mtiMaskImage)
let ciImage = CIImage(cgImage: image.cgImage!)
let mtiImage = MTIImage(ciImage: ciImage,isOpaque: true)
let contextOptions = MTIContextOptions()
let context = try! MTIContext(device: MTLCreateSystemDefaultDevice()!,options: contextOptions)
let blendFilter = MTIBlendWithMaskFilter()
blendFilter.inputMask = mtiMask
blendFilter.inputBackgroundImage = mtiMaskImage
blendFilter.inputimage = mtiImage
let outputimage = try! context.makeCGImage(from: blendFilter.outputimage!)
return UIImage(cgImage: outputimage)
}
这似乎是我对预乘Alpha的误解或误用。
输入图片:
蒙版图像:
输出图像:
解决方法
您将混合滤镜的backgroundImage设置为遮罩不透明的图像。要获得透明背景,请将backgroundImage设置为透明颜色。
blendFilter.inputBackgroundImage = MTIImage.init(color: MTIColor.clear,sRGB: false,size: mtiImage.size)
MTIBlendWithMaskFilter
的输出图像尺寸将等于inputBackgroundImage
的尺寸。根据需要设置大小。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。