iOS 17 issues getting dominantColors
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?
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
Apparently i cant reproduce the issue if i use best DominantColorQuality
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.
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)
}
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
}
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?