最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - SwiftUI - CIEdgeWork Filter Returns Blank Image - Stack Overflow

programmeradmin3浏览0评论

I'm trying to apply the CIEdgeWork filter in a SwiftUI project, but I'm getting a blank (white) image instead of the expected effect. Here is my implementation:

Code:

import SwiftUI
import CoreImage
import CoreImage.CIFilterBuiltins

struct EdgeWorkDemo: View {
    let context = CIContext()

    var body: some View {
        VStack {
            if let outputImage = applyEdgeWorkFilter() {
                Image(uiImage: outputImage)
                    .resizable()
                    .scaledToFit()
            } else {
                Text("No Image")
            }
        }
    }

    func applyEdgeWorkFilter() -> UIImage? {
        guard let inputImage = UIImage(named: "imgBG") else {
            print("❌ Image not found")
            return nil
        }
        guard let ciImage = CIImage(image: inputImage) else {
            print("❌ Failed to create CIImage")
            return nil
        }

        let edgeWorkFilter = CIFilter.edgeWork()
        edgeWorkFilter.inputImage = ciImage
        edgeWorkFilter.radius = 40 // area of effect

        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("❌ Failed to create CGImage")
        return nil
    }
}

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

What I've Tried:

  • Confirmed that "imgBG" is present in the asset catalog.
  • Tried reducing the radius to 5 and 10 but still getting a blank output.
  • Wrapped the image with CIAffineClamp before applying CIEdgeWork to avoid transparent edges.
  • Tested other filters like CISepiaTone, which work correctly.
  • Used different image formats (JPEG, PNG).
  • Ran on both the simulator and a real device.

Expected Behavior: I expect the CIEdgeWork filter to extract edges from the image, similar to how it's described in the documentation: Apple Docs - CIEdgeWork.

Question:

  • Why is CIEdgeWork returning a blank image?
  • Are there any known limitations with this filter in SwiftUI?
  • Is there an alternative way to achieve a similar effect?

this is the image i processing using this

My Image

发布评论

评论列表(0)

  1. 暂无评论