I need some help to create a smart detector that can detect if a UIImage
is light or dark, so that I can but on a UILabel
with a textColor
in the opposite color, either black or white.
Can this be done in a few lines or do I need a hardcore CoreImage
manoeuvre?
Thanks!
Seeing if the image is "dark" or "light" won't work generally. The part you end up putting the text on could have both light and dark parts even if the overall image might be considered one or the other.
A common solution to adding text to a random picture is to add a partially transparent black background to the white text (or a partially transparent white background to black text).
If you have a random image, you will have to inspect all the pixels. You can't just inspect some pixels - as a counterexample, you can imagine an image that is entirely white with only some black pixels and you inspect only them. That would give you bad results.
The simplest solution I can imagine would be to convert the image to grayscale and then go through all the pixels, calculating the average gray. Then you can decide if the average is closer to white or closer to black.
You can also downscale the image before processing (drawing the image into a smaller image).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.