Core Image TileEffect过滤器在swift中返回nil图像

时间:2017-07-10 13:02:13

标签: ios swift3 core-image cifilter xcode8.2

我正在为CI过滤器创建演示应用程序,几乎应用了所有类别中的所有过滤器。但是当我尝试在CICategoryTileEffect中添加过滤器时,除了" CIKaleidoscope"之外,所有过滤器都返回nil图像。所有其他过滤器都崩溃了。它是展示代码的类的一部分。

enum EN_TileEffect :String
{
        case affineClamp =  "AffineClamp"
        case affineTile =  "AffineTile"
        case eightfoldReflectedTile =  "EightfoldReflectedTile"
        case fourfoldReflectedTile =  "FourfoldReflectedTile"
        case fourfoldRotatedTile =  "FourfoldRotatedTile"
        case fourfoldTranslatedTile =  "FourfoldTranslatedTile"
        case kaleidoscope =  "Kaleidoscope"
        case opTile =  "OpTile"
        case parallelogramTile =  "ParallelogramTile"
        case perspectiveTile =  "PerspectiveTile"
        case sixfoldReflectedTile =  "SixfoldReflectedTile"
        case triangleKaleidoscope =  "TriangleKaleidoscope"
        case triangleTile =  "TriangleTile"
        case twelvefoldReflectedTile =  "TwelvefoldReflectedTile"
}

class TileEffect {

    fileprivate static var filter : CIFilter!
    fileprivate static var context : CIContext = CIContext(options:nil)

    static func applyFilter(filter: EN_TileEffect , image: UIImage) -> UIImage {
        switch filter {
            case .affineClamp: return affineClamp(image: image)
            case .kaleidoscope: return kaleidoscope(image: image)
            default : return image
        }
    }

    //Just working fine and as expected
    fileprivate static func kaleidoscope(image:UIImage) -> UIImage {

        let originalImage = CIImage(cgImage: (image.cgImage)!)
        let orientation = (image.imageOrientation)

        filter = CIFilter(name: "CIKaleidoscope")
        filter?.setValue(originalImage, forKey: kCIInputImageKey)
        filter?.setValue(CIVector(x:120,y:120), forKey: kCIInputCenterKey)
        filter?.setValue(0, forKey: kCIInputAngleKey)
        filter?.setValue(2, forKey: "inputCount")

        let cgimg = context.createCGImage(filter!.outputImage!, from: filter!.outputImage!.extent)
        return UIImage(cgImage: cgimg!, scale: 1.0, orientation: orientation)
    }
   // This method could not generate a new image and crashing every time.
    fileprivate static func affineClamp(image:UIImage) -> UIImage {

        let originalImage = CIImage(cgImage: (image.cgImage)!)
        let orientation = (image.imageOrientation)

        filter = CIFilter(name: "CIAffineClamp")
        filter?.setValue(originalImage, forKey: kCIInputImageKey)

        let transform = CGAffineTransform.identity
        let val = NSValue(cgAffineTransform: transform)
        filter.setValue(val, forKey: "inputTransform")

        **//Always returning nil image here**
        let cgimg = context.createCGImage(filter!.outputImage!, from: filter!.outputImage!.extent)

        return UIImage(cgImage: cgimg!, scale: 1.0, orientation: orientation)
    }
}

我搜索了很多,但没有找到任何解决方案或工作示例。

1 个答案:

答案 0 :(得分:1)

谢谢大家。替换一行代码解决了我的问题 来自

let cgimg = context.createCGImage(filter!.outputImage!, from: filter!.outputImage!.extent)

 let cgimg = context.createCGImage(filter!.outputImage!, from: originalImage.extent)