CIFilters显示剪切的高光和阴影

时间:2018-04-13 15:41:42

标签: ios swift filter cifilter ciimage

在图像编辑应用程序中,我尝试使用CIFilter显示剪切的高光和阴影。

enter image description here

Filter List

我知道没有一个直接的单一过滤器,必须是几个在一起的组合。

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

CIFilters可以一个接一个地添加。例如:我可以先调整图像上的阴影然后裁剪它。

func addFilters(toImage image: UIImage) -> UIImage? {
    guard let cgImage = image.cgImage else { return nil }
    let ciImage = CIImage(cgImage: cgImage)

    //add adjust shadow filter
    guard let shadowAdjust = CIFilter(name: "CIHighlightShadowAdjust") else { return nil }
    shadowAdjust.setValue(ciImage, forKey: kCIInputImageKey)
    shadowAdjust.setValue(1, forKey: "inputHighlightAmount")
    shadowAdjust.setValue(3, forKey: "inputShadowAmount")
    guard let output1 = shadowAdjust.outputImage else { return nil }

    //now output result crop 80%
    guard let crop = CIFilter(name: "CICrop") else { return nil }
    crop.setValue(ciImage, forKey: kCIInputImageKey)
    crop.setValue(CIVector(x: output1.extent.origin.x, y: output1.extent.origin.y, z: output1.extent.size.width * 0.8, w: output1.extent.size.height * 0.8), forKey: "inputRectangle")
    guard let output2 = crop.outputImage else { return UIImage(ciImage: output1) }

    //the image will be croped and shadow adjusted
    return UIImage(ciImage: output2)
}