评估 UIImage 中的黑暗度
我想运行一个计算来看看 UIImage 的底部 20px 通常是暗的还是亮的。我在该图像的顶部显示少量文本(底部一行,即底部 20 像素),并且想动态确定是否应该将此文本设为黑色或白色。
我猜我应该能够分析图像中我感兴趣的区域的每个像素,然后对所有像素进行平均以确定图像在该区域中通常是亮还是暗。显然,这不是一门精确的科学,但它比必须在 300 多张图像上定义是否应该为每个图像显示浅色或深色文本要好。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 CGBitmapContextCreate 来创建由您控制的缓冲区支持的上下文。使用 CGContextDrawImage 将图像绘制到缓冲区中。循环遍历缓冲区中的每个像素以计算平均颜色并选择适当的对比颜色。
Use
CGBitmapContextCreate
to create a context backed by a buffer you control. Draw the image into the buffer usingCGContextDrawImage
. Loop through each pixel in the buffer to calculate the average color and choose an appropriate contrast color.