Chanchal Warde
Chanchal Warde

Reputation: 983

Core Image TileEffect filters in swift are returning nil image

I am creating demo app for CI Filters and almost applied all filters in all categories. But when I tried to add filters in CICategoryTileEffect all filters are returning nil image except "CIKaleidoscope". All other filters are crashing. It's a part of the class to show the code.

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)
    }
}

I searched a lot but didn't find any solution or working example.

Upvotes: 0

Views: 803

Answers (1)

Chanchal Warde
Chanchal Warde

Reputation: 983

Thanks, everyone. Replacing one line code solved my problem From

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

To

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

Upvotes: 1

Related Questions