2015-11-14 102 views
1

我有一个UILabel,放置在UIImageView的顶部。 UIImageView可以改变显示各种图像。根据UIImageView中显示的图像,UILabel的文本颜色必须能够动态更改。例如,当显示浅色图像时,将使用黑色文本颜色,或者使用黑色图像时,将使用浅色文本颜色。获取Swift中UILabel后面UIImageView中像素的RGB值或平均RGB值

在夫特,什么是最好的,简单的,最有效的方法来提取一组正后方的UILabel即坐在UIImage上方的位置的像素的单个像素或平均RGB值的RGB值?

或者更好,在Swift中,是否有一个UILabel方法根据它位于上方的背景动态地改变文本颜色?

谢谢。

enter image description here

+0

你应该更清楚!你需要平均颜色吗?或最上面的颜色?或颜色在哪里接触? –

+0

http://stackoverflow.com/questions/12770181/how-to-get-the-pixel-color-on-touch –

+0

对不起,如果我不清楚@T先生UILabel地区的平均颜色是需要什么。 (因此,例如,如果UILabel是50x10像素,则平均所需的区域也将与UIImageView完全相同的位置为50x10像素。)感谢您的链接,它指的是单个像素。我认为平均值会更强劲。任何有关完整而简单的Swift答案的建议。 – user4806509

回答

0

老实说,我甚至不抢RGB,你应该知道你把什么样的形象进入的UIImageView,所以计划基于这样的标签。

如果你必须选择RGB,那么这样做,事情是这样的:

UIGraphicsBeginImageContext(CGSizeMake(1,1)); 
    let context = UIGraphicsGetCurrentContext() 
    //We translate in the opposite direction so that when we draw to the canvas, it will draw our point at the first spot in readable memory 
    CGContextTranslateCTM(context, -x, -y); 
    // Make the CALayer to draw in our "canvas". 
    self.layer.renderInContext(context!); 

    let colorpoint = UnsafeMutablePointer<UInt32>(CGBitmapContextGetData(context)); 
    UIGraphicsEndImageContext(); 

其中X和Y是你想抓住点,自我是你的UIImageView。

编辑:@ Mr.T发布一个链接到如何做到这一点为好,如果你需要的平均,只是抓住改变UIGraphicsBeginImageContext(CGSizeMake(1,1));UIGraphicsBeginImageContext(CGSizeMake(width,height));所需的像素量,并用这些数据计算平均