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

dominantColors function crash for particular image #32

Open
NatashaLavrinenko opened this issue Jan 16, 2024 · 1 comment
Open

dominantColors function crash for particular image #32

NatashaLavrinenko opened this issue Jan 16, 2024 · 1 comment

Comments

@NatashaLavrinenko
Copy link

Hey!
Found a bug for the following function:

extension UIImage {
 func getDominantColors(with minCount: Int = 0) -> [UIColor]? {
        var resultColors: [UIColor]?
        // Try With PNG Data
        if let pngData = pngData(),
           let pngImage = UIImage(data: pngData),
           let colors = try? pngImage.dominantColors(with: .best, algorithm: .iterative) {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        // Try With JPEG Data
        if let jpegData = jpegData(compressionQuality: 1),
           let jpegImage = UIImage(data: jpegData),
           let colors = try? jpegImage.dominantColors(with: .best, algorithm: .iterative) {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        // Try With Original Image
        if let colors = try? dominantColors(with: .best, algorithm: .iterative) {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        if let colors = try? dominantColors() {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        return resultColors
    }
}

When the following image is added as input parameter, crash inside ColorKit happened:
Снимок экрана 2024-01-16 в 16 12 20

Please find the image here

Image -
IMG_6111

Could this issue be fixed? Looks like the safe check for dictionary should be added. Thanks in advance!
@terribleben
Copy link

I was hitting this crash for all images captured on my iOS 17 device. Patching this pull request fixed it for me: https://github.com/Boris-Em/ColorKit/pull/28/files

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

2 participants