Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS 17 issues getting dominantColors #27

Open
gbuela opened this issue Jul 31, 2023 · 6 comments
Open

iOS 17 issues getting dominantColors #27

gbuela opened this issue Jul 31, 2023 · 6 comments

Comments

@gbuela
Copy link

gbuela commented Jul 31, 2023

I'm testing my app with the iOS 17 public beta and I get totally unexpected dominant colors, whereas on my device with iOS 16 I still see them fine.
It could be a bug of the beta but it could also be that there's something fundamentally different in UIImage now. Can you guys check that?

@sahilchaddha
Copy link

I am on the same boat with iOS 17 Beta 4. It works on iOS 16 but all dominant colors on iOS 17 are quite different and unexpected

@sahilchaddha
Copy link

Apparently i cant reproduce the issue if i use best DominantColorQuality

@jakezien
Copy link

jakezien commented Sep 6, 2023

Same boat here, and many images just return no colors at all for dominantColors(). best quality helps, but Doesn't solve the problem; in particular it seems to have trouble returning reddish colors. Here are some example screenshots from the simulator that show an example image + the colors returned from dominantColors.
image
image
image

@nryrk
Copy link

nryrk commented Oct 2, 2023

I also discovered the bug and have a fix for it. The RGB value of the dominantColor appears to be swapped.

  if #available(iOS 17.0, *) {
      /* iOS 17 solution for:
       * let pixelColor = RGB(R: data[index + 0], G: data[index + 1], B:  data[index + 2])
       * colorsCountedSet.add(pixelColor)
       *
       * Needed to switch first and last element since iOS 17.0
       */
      let pixelColor = RGB(R: data[index + 2], G: data[index + 1], B:  data[index + 0])
      colorsCountedSet.add(pixelColor)
  } else {
      let pixelColor = RGB(R: data[index + 0], G: data[index + 1], B:  data[index + 2])
      colorsCountedSet.add(pixelColor)
  }

@DaniShalash
Copy link

DaniShalash commented Oct 12, 2023

I'm not an expert in image and color processing (hence using this package), however, given I'm using CoreData, I have a case in my app where I'm saving UIImage in my CoreData DataBase as Data, and when retrieving it, I'm casting it back to UIImage using ValueTransformer.

By coincidence, what I have observed that on iOS 17 when I get the dominant colors of a UIImage selected by the user (Before saving it to CoreData), I get inverted colors as @nryrk observed.

But when I close the app and open it again (Getting the image from CoreData) and try to get the dominant colors from the same image, I get the right NOT inverted colors!!

Upon checking my ValueTransformer, I have noticed that the data I'm saving to my core data is image.pngData()

So what I have tried is actually before getting the dominant colors of any image (Just to test), I have used derived temp image like this let derivedImage = UIImage(data: image.pngData()!)! and now dominant colors are always right and NOT inverted.

I didn't dig deeper yet, but I thought I might share this update help identifying the root cause of the issue faster, maybe someone can correlate it and find solid solution.

Update:

For now, as a temporary solution, I have wrote this helper function which seems to work on iOS 15, 16 and 17 (Didn't test on iOS 14 and lower) until I can find the root cause and its solution:

func getDominantColors(from image: UIImage) -> [UIColor]? {
        /** Try With PNG Data */
        if
            let pngData = image.pngData(),
            let pngImage = UIImage(data: pngData),
            let colors = try? pngImage.dominantColors(with: .best, algorithm: .iterative),
            !colors.isEmpty {
            return colors
        }
        /** Try With JPEG Data */
        if
            let jpegData = image.jpegData(compressionQuality: 1),
            let jpegImage = UIImage(data: jpegData),
            let colors = try? jpegImage.dominantColors(with: .best, algorithm: .iterative),
            !colors.isEmpty {
            return colors
        }
        /** Try With Original Image */
        if let colors = try? image.dominantColors(with: .best, algorithm: .iterative), !colors.isEmpty {
            return colors
        }
        /** Unfortuante!!! */
        return nil
    }

@danniss10
Copy link

I'm also having this issue. Is there any chance this gets patched in the main repo? Does anyone here have a forked version that we can use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants