I’ve the beneath easy code for CIPersonSegmentation
if let picture = UIImage(named: "demo9"), let editted = applyPersonFilter(to: picture) {
imageView.picture = editted
}
func applyPersonFilter(to picture: UIImage) -> UIImage? {
guard let ciImage = CIImage(picture: picture) else { return nil }
let context = CIContext(choices: nil)
let filter = CIFilter(title: "CIPersonSegmentation", parameters: [
kCIInputImageKey: ciImage,
"inputQualityLevel": 1.0
])
guard let outputImage = filter?.outputImage else {
return nil
}
print("outputImage: (outputImage.extent)")
guard let cgImage = context.createCGImage(outputImage, from: outputImage.extent) else { return nil }
return UIImage(cgImage: cgImage)
}
This merely crashes at createCGImage
with:
Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
Be aware that the print
earlier than context.createCGImage
logs (0.0, 0.0, 512.0, 384.0)
.
If I change the "CIPersonSegmentation"
with another filter title, then it really works effective.
EDIT:
I think I’ll have found out the issue. I used to be working the above code on the simulator when it was crashing. Then I got here throughout this text when researching another picture modifying:
https://www.artemnovichkov.com/weblog/remove-background-from-image-in-swiftui
It says:
“The requests might fail. For instance, handler throws an error should you attempt to run the code on an iOS Simulator: 🚫 Area=com.apple.Imaginative and prescient Code=9 “Couldn’t create inference context” UserInfo={NSLocalizedDescription=Couldn’t create inference context}”
Be aware that the above is about the usage of Imaginative and prescient
API and never CIFilter. Nonetheless, I suspected that the CIPersonSegmentation
additionally makes use of Imaginative and prescient
API behind the scenes. Therefore the crash on simulator.
It really works effective on system.
Can somebody verify my suspicion?