guoyingtao / Mantis

An iOS Image cropping library, which mimics the Photo App written in Swift.
MIT License
921 stars 183 forks source link

Error: CGBitmapContextInfoCreate: CGColorSpace which uses extended range requires floating point or CIF10 bitmap context #374

Closed bgalbs closed 8 months ago

bgalbs commented 8 months ago

Discussed in https://github.com/guoyingtao/Mantis/discussions/373

Originally posted by **bgalbs** March 2, 2024 Hi! I’m incorporating Mantis into a simple SwiftUI app, and testing it on an iPhone 15 Pro (both real device and simulator). On both devices, running iOS 17.3.1, I get the following error when Mantis tries to transform the image (usually just a simple rect crop): assertionFailure: croppedImage should not be nil This assertion fails in CropView.addImageMask(to:), line 676 It appears related to this warning that is printed to the console when the CGContext is created on line 47 in the CGImage.transformedImage extension defined in CGImageExtensions: CGBitmapContextInfoCreate: CGColorSpace which uses extended range requires floating point or CIF10 bitmap context Any tips on what might be going wrong here? This happens when I use many of the sample images in the stock iPhone 15 Pro simulator, as well as many of the images on my test iPhone. Thanks... Ben
bgalbs commented 8 months ago

I should have mentioned: I’m using the Mantis 2.18.0 package.

guoyingtao commented 8 months ago

@bgalbs Thanks for the report! Can you help to answer the questions below? Did it only happen on iOS 17.3.1? Have tested on previous iOS version too? Did it happen to any images on iOS 17.3.1?

guoyingtao commented 8 months ago

@bgalbs Would it be OK for you to post some sample images here which caused the assertionFailure? Thank you!

bgalbs commented 8 months ago

Sure! Here’s one image. IMG_2203.HEIC.zip

bgalbs commented 8 months ago

And, forgive me, the iPhone 15 Pro simulator is running iOS 17.2, and the physical iPhone 15 Pro I’m testing on is running iOS 17.3.1. I’ll try it on an iOS 16.4 simulator and see what happens. Thanks!

guoyingtao commented 8 months ago

https://github.com/guoyingtao/Mantis/assets/26723384/9f21ced6-b146-4cde-85b3-fd1ba7105510

Just tested the image from you on my iPhone(iOS 17.3.1) it worked fine. But I don't have iPhone 15 Pro in hand. My iPhone is an old one.

bgalbs commented 8 months ago

Oh, interesting. I’ll either use the sample code to crop the image, or write a simple test harness just for this use case, and see if it works for me. Thank you!

bgalbs commented 8 months ago

Here’s a sample project I made that demonstrates the error. My guess is that this snippet here is the culprit:

                let renderer = ImageRenderer(content: profileImage)
                if let image = renderer.uiImage {
                    self.image = renderer.uiImage
                    self.showCropper = true
                }

This is where I convert the SwiftUI Image into a uiImage for use with Mantis. I have a feeling this is not what I should be doing.

MantisTest.zip

bgalbs commented 8 months ago

Ok, I figured out my problem. I was using SwiftUI’s PhotosPicker to load an image into the SwiftUI Image format, and then using ImageRenderer to convert the SwiftUI Image to a UIImage.

Ignoring the inefficiency of this approach, there was something about the UIImage that was causing this error. When I use UIImagePickerController and receive a UIImage back from the photo picker, Mantis crops it no problem.

So I’ll either update my code to have PhotosPicker return the image data I can use to create a UIImage directly, or I’ll update my project to use UIImagePickerController.

Thanks again for the quick response.