【问题标题】:Status bar style from background image color percent iOS来自背景图像颜色百分比 iOS 的状态栏样式
【发布时间】:2015-05-07 04:08:52
【问题描述】:

我很有趣,获取背景图像颜色百分比的算法是什么。如果背景图像是黑色苹果将状态栏设置为白色,如果背景图像是白色则变为黑色。如果您有一些示例如何在应用程序中执行此操作,请帮助我。

我将上传示例(背景是图像,而不是颜色)。

【问题讨论】:

    标签: ios iphone statusbar


    【解决方案1】:

    如何获取平均颜色然后应用逆?

    寻找平均颜色(来自Bobby Georgescu's blog
    + 寻找反色(来自a * answer


    /*
     UIImage+AverageColor.m
    
     Copyright (c) 2010, Mircea "Bobby" Georgescu
     All rights reserved.
    
     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
     * Neither the name of the Mircea "Bobby" Georgescu nor the
     names of its contributors may be used to endorse or promote products
     derived from this software without specific prior written permission.
    
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     DISCLAIMED. IN NO EVENT SHALL Mircea "Bobby" Georgescu BE LIABLE FOR ANY
     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */
    
    #import "UIImage+AverageColor.h"
    
    @implementation UIImage (AverageColor)
    
    - (UIColor *)averageColor {
    
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char rgba[4];
        CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
        CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage);
        CGColorSpaceRelease(colorSpace);
        CGContextRelease(context);  
    
        if(rgba[3] > 0) {
            CGFloat alpha = ((CGFloat)rgba[3])/255.0;
            CGFloat multiplier = alpha/255.0;
            return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier
                                   green:((CGFloat)rgba[1])*multiplier
                                    blue:((CGFloat)rgba[2])*multiplier
                                   alpha:alpha];
        }
        else {
            return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0
                                   green:((CGFloat)rgba[1])/255.0
                                    blue:((CGFloat)rgba[2])/255.0
                                   alpha:((CGFloat)rgba[3])/255.0];
        }
    }
    
    -(UIColor*) invertColor:(UIColor *)color
    {
        CGFloat r,g,b,a;
        [color getRed:&r green:&g blue:&b alpha:&a];
        return [UIColor colorWithRed:1.-r green:1.-g blue:1.-b alpha:a];
    }
    
    - (UIColor *)statusBarColor {
      return [self invertColor:[self averageColor]];
    }
    @end
    

    【讨论】:

    • 太好了,谢谢,我使用了一些其他代码来获得平均颜色,它可以工作)