swift - iOS Core Image CIPersonSegmentation filter crashes at context.createCGImage with EXC_BAD_ACCESS - Stack Overflow

I have the below simple code for CIPersonSegmentationif let image = UIImage(named: "demo9"),

I have the below simple code for CIPersonSegmentation

if let image = UIImage(named: "demo9"), let editted = applyPersonFilter(to: image) {
    imageView.image = editted
}

func applyPersonFilter(to image: UIImage) -> UIImage? {
    guard let ciImage = CIImage(image: image) else { return nil }
    
    let context = CIContext(options: nil)
    
    let filter = CIFilter(name: "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 simply crashes at createCGImage with:

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

Note that the print before context.createCGImage logs (0.0, 0.0, 512.0, 384.0).

If I replace the "CIPersonSegmentation" with some other filter name, then it works fine.

EDIT:

I suspect I may have figured out the problem. I was running the above code on the simulator when it was crashing. Then I came across this article when researching some other image editing:

It says:

"The requests may fail. For example, handler throws an error if you try to run the code on an iOS Simulator:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744859143a4597591.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信