来自背景图像颜色百分比iOS的状态栏样式

来自背景图像颜色百分比iOS的状态栏样式,ios,iphone,statusbar,Ios,Iphone,Statusbar,我很感兴趣,获取背景图像颜色百分比的算法是什么。若背景图像为黑色,则苹果将状态栏设置为白色,若背景图像为白色,则状态栏将变为黑色。如果你有一些例子如何在应用程序中做到这一点,请帮助我 我将上传样本(背景是图像,而不是颜色)。 获取平均颜色,然后应用相反的颜色如何 查找平均颜色(来自) +查找反向颜色(来自) 太好了,谢谢你,我使用了一点其他代码来获得平均颜色,而且效果很好) /* UIImage+AverageColor.m Copyright (c) 2010, Mircea "Bo

我很感兴趣,获取背景图像颜色百分比的算法是什么。若背景图像为黑色,则苹果将状态栏设置为白色,若背景图像为白色,则状态栏将变为黑色。如果你有一些例子如何在应用程序中做到这一点,请帮助我

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


获取平均颜色,然后应用相反的颜色如何

查找平均颜色(来自)
+查找反向颜色(来自)



太好了,谢谢你,我使用了一点其他代码来获得平均颜色,而且效果很好)
/*
 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