tags:

views:

27

answers:

1

I would like to run a calculation to see if the bottom 20px of a UIImage are generally dark or light. I am displaying a small amount of text on top of this image (one line at the bottom, ie. the bottom 20px), and would like to determine dynamically whether I should make this text black or white.

I am guessing I should be able analyze each pixel in the image for just the region I'm interested in, and then average across all the pixels to determine if the image is generally light or dark in that region. Obviously this won't be an exact science, but it would be better than having to define on 300+ images whether I should show light or dark text for each.

Any ideas?

A: 

Use CGBitmapContextCreate to create a context backed by a buffer you control. Draw the image into the buffer using CGContextDrawImage. Loop through each pixel in the buffer to calculate the average color and choose an appropriate contrast color.

rpetrich