import SwiftUI
struct CIFilterDemoView: View {
let monalisa: UIImage = UIImage(named: "monalisa")!
@State var filtedImage: CGImage?
func testEVFilter() {
guard let rawFilter = CIRAWFilter(imageData: monalisa.pngData()) else { return }
rawFilter.setValue(3, forKey: kCIInputEVKey)
guard let image = rawFilter.outputImage else { return }
guard let cgImage = CIContext().createCGImage(image, from: image.extent) else { return }
self.filtedImage = cgImage
}
func testMatte() {
guard let rawFilter = CIRAWFilter(imageData: monalisa.pngData()) else { return }
guard let hairImage = rawFilter.semanticSegmentationHairMatte else { return }
guard let cgImage = CIContext().createCGImage(hairImage, from: hairImage.extent) else { return }
self.filtedImage = cgImage
}
var body: some View {
List {
Image(uiImage: monalisa)
HStack {
Button {
testEVFilter()
} label: {
Text("Test EV")
}
.buttonStyle(.borderedProminent)
Button {
testMatte()
} label: {
Text("Test Matte")
}
.buttonStyle(.borderedProminent)
}
if let filtedImage, let uiImage = UIImage(cgImage: filtedImage) {
Image(uiImage: uiImage)
.background(Color.red)
}
}
}
}
struct CIFilterDemoView_Previews: PreviewProvider {
static var previews: some View {
CIFilterDemoView()
}
}
When I run testMatte()
get this error log:
2023-01-26 19:42:45.682098+0800 DemoApp[28473:1085301] -[CIRAWFilterImpl semanticSegmentationHairMatte]: unrecognized selector sent to instance 0x7ff0a8740130
2023-01-26 19:42:45.682583+0800 DemoApp[28473:1085301] [General] -[CIRAWFilterImpl semanticSegmentationHairMatte]: unrecognized selector sent to instance 0x7ff0a8740130
2023-01-26 19:42:45.688117+0800 DemoApp[28473:1085301] [General] (
0 CoreFoundation 0x00007ff80ca8d3eb __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007ff80c5d9e25 objc_exception_throw + 48
2 CoreFoundation 0x00007ff80cb2452b -[NSObject(NSObject) __retain_OA] + 0
3 CoreFoundation 0x00007ff80c9f762b ___forwarding___ + 1324
4 CoreFoundation 0x00007ff80c9f7068 _CF_forwarding_prep_0 + 120
5 DemoApp 0x00000001016584af $s7DemoApp08CIFilterA4ViewV9testMatteyyF + 527
6 DemoApp 0x00000001016598c8 $s7DemoApp08CIFilterA4ViewV4bodyQrvg7SwiftUI05TupleD0VyAE5ImageV_AE6HStackVyAGyAE0D0PAEE11buttonStyleyQrqd__AE015PrimitiveButtonL0Rd__lFQOyAE0N0VyAE4TextVG_AE017BorderedProminentnL0VQo__AWtGGAmEE10background_20ignoresSafeAreaEdgesQrqd___AE4EdgeO3SetVtAE05ShapeL0Rd__lFQOyAI_AE5ColorVQo_SgtGyXEfU_AXyXEfU_yycfU1_ + 40
7 SwiftUI 0x00007ffa0e439df8 __swift_memcpy160_4 + 111106
8 SwiftUI 0x00007ffa0e43a799 __swift_memcpy160_4 + 113571
9 SwiftUI 0x00007ffa0e43a70b __swift_memcpy160_4 + 113429
10 SwiftUI 0x00007ffa0ec1c9d7 __swift_memcpy36_4 + 41329
11 SwiftUI 0x00007ffa0e84b8e4 objectdestroy.142Tm + 42261
12 SwiftUI 0x00007ffa0e84b8f8 objectdestroy.142Tm + 42281
13 SwiftUI 0x00007ffa0e84b8e4 objectdestroy.142Tm + 42261
14 SwiftUI 0x00007ffa0e57a829 block_destroy_helper.15 + 49718
15 SwiftUI 0x00007ffa0e57a18c block_destroy_helper.15 + 48025
16 SwiftUI 0x00007ffa0e6eb2d4 _callVisitToolbarContentType2 + 4283
17 SwiftUI 0x00007ffa0eeffd7d _callVisitStyleContextType2 + 11403
18 SwiftUI 0x00007ffa0eefe3f6 _callVisitStyleContextType2 + 4868
19 SwiftUI 0x00007ffa0eefe4da _callVisitStyleContextType2 + 5096
20 SwiftUI 0x00007ffa0eefdccd _callVisitStyleContextType2 + 3035
21 UIKitCore 0x00007ff9182ce09a -[UIGestureRecognizer _componentsEnded:withEvent:] + 162
22 UIKitCore 0x00007ff917b91620 -[UITouchesEvent _sendEventToGestureRecognizer:] + 776
23 UIKitCore 0x00007ff917b73c1e -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 247
24 UIKitCore 0x00007ff917b73655 -[UIGestureEnvironment _updateForEvent:window:] + 188
25 UIKitCore 0x00007ff917b7333f -[UIWindow sendEvent:] + 5301
26 UIKitCore 0x00007ff917b7131c -[UIApplication sendEvent:] + 984
27 UIKit 0x00007ffb2c015b78 -[UIApplicationAccessibility sendEvent:] + 85
28 UIKitCore 0x00007ff917b6f145 __dispatchPreprocessedEventFromEventQueue + 10186
29 UIKitCore 0x00007ff91883f061 __processEventQueue + 8273
30 UIKitCore 0x00007ff918837724 __eventFetcherSourceCallback + 249
31 CoreFoundation 0x00007ff80ca14b78 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
32 CoreFoundation 0x00007ff80ca14b27 __CFRunLoopDoSource0 + 157
33 CoreFoundation 0x00007ff80ca14901 __CFRunLoopDoSources0 + 212
34 CoreFoundation 0x00007ff80ca1357b __CFRunLoopRun + 929
35 CoreFoundation 0x00007ff80ca12b60 CFRunLoopRunSpecific + 560
36 HIToolbox 0x00007ff816367766 RunCurrentEventLoopInMode + 292
37 HIToolbox 0x00007ff816367576 ReceiveNextEventCommon + 679
38 HIToolbox 0x00007ff8163672b3 _BlockUntilNextEventMatchingListInModeWithFilter + 70
39 AppKit 0x00007ff80fb63233 _DPSNextEvent + 909
40 AppKit 0x00007ff80fb620b4 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1219
41 AppKit 0x00007ff80fb546f7 -[NSApplication run] + 586
42 AppKit 0x00007ff80fb28727 NSApplicationMain + 817
43 AppKit 0x00007ff80fde9856 _NSApplicationMainWithInfoDictionary + 16
44 UIKitMacHelper 0x00007ff823ab35d3 UINSApplicationMain + 1413
45 UIKitCore 0x00007ff9178df688 UIApplicationMain + 144
46 SwiftUI 0x00007ffa0eeb7787 __swift_memcpy93_8 + 11978
47 SwiftUI 0x00007ffa0eeb7632 __swift_memcpy93_8 + 11637
48 SwiftUI 0x00007ffa0e52aedf __swift_memcpy195_8 + 12258
49 DemoApp 0x0000000101656cee $s7DemoApp0abB0V5$mainyyFZ + 30
50 DemoApp 0x0000000101656df9 main + 9
51 dyld 0x00007ff80c606310 start + 2432
)
This seems like a bug in the CIRAWFilter
implementation. It would be great if you could file a bug report in the Feedback app for that. Thanks!
A conceptual note: The CIRAWFilter
is meant to be initialized with RAW image data. You are passing it PNG data, which is not what it was designed for. It's a bit surprising that it even works with non-RAW images.
If you want to read the auxiliary data embedded in an image, you can instead do the following:
let hairMatte = CIImage(contentsOf: imageFileURL, options: [CIImageOption.auxiliarySemanticSegmentationHairMatte: true])
This should work with most CIImage
initializers that provide the options
parameter. Though I'm not sure if it would work if you load the image with UIImage(named:)
as it might strip the auxiliary data on load.
Check out CIImageOption for available aux data to load.