-0.4 C
New York
Saturday, February 22, 2025

ios – SwiftUI – CIEdgeWork Filter Returns Clean Picture


I am making an attempt to use the CIEdgeWork filter in a SwiftUI mission, however I am getting a clean (white) picture as an alternative of the anticipated impact. Right here is my implementation:

Code:

import SwiftUI
import CoreImage
import CoreImage.CIFilterBuiltins

struct EdgeWorkDemo: View {
    let context = CIContext()

    var physique: some View {
        VStack {
            if let outputImage = applyEdgeWorkFilter() {
                Picture(uiImage: outputImage)
                    .resizable()
                    .scaledToFit()
            } else {
                Textual content("No Picture")
            }
        }
    }

    func applyEdgeWorkFilter() -> UIImage? {
        guard let inputImage = UIImage(named: "imgBG") else {
            print("❌ Picture not discovered")
            return nil
        }
        guard let ciImage = CIImage(picture: inputImage) else {
            print("❌ Didn't create CIImage")
            return nil
        }

        let edgeWorkFilter = CIFilter.edgeWork()
        edgeWorkFilter.inputImage = ciImage
        edgeWorkFilter.radius = 40 // space of impact

        guard let resultImage = edgeWorkFilter.outputImage else {
            print("❌ Filter output is nil")
            return nil
        }

        let context = CIContext()
        if let cgImage = context.createCGImage(resultImage, from: resultImage.extent) {
            return UIImage(cgImage: cgImage)
        }

        print("❌ Didn't create CGImage")
        return nil
    }
}

struct EdgeWorkDemo_Previews: PreviewProvider {
    static var previews: some View {
        EdgeWorkDemo()
    }
}

What I’ve Tried:

  • Confirmed that “imgBG” is current within the asset catalog.
  • Tried decreasing the radius to five and 10 however nonetheless getting a clean output.
  • Wrapped the picture with CIAffineClamp earlier than making use of CIEdgeWork to keep away from clear edges.
  • Examined different filters like CISepiaTone, which work accurately.
  • Used completely different picture codecs (JPEG, PNG).
  • Ran on each the simulator and an actual machine.

Anticipated Conduct:
I count on the CIEdgeWork
filter to extract edges from the picture, just like the way it’s described within the documentation:
Apple Docs – CIEdgeWork.

Query:

  • Why is CIEdgeWork returning a clean picture?
  • Are there any recognized limitations with this filter in SwiftUI?
  • Is there an alternate option to obtain an identical impact?

that is the picture i processing utilizing this

My Picture

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles