如何根据视图图层的颜色更改状态栏颜色

时间:2016-05-22 13:01:50

标签: ios swift colors

我有一个使用AVCaptureSession将相机上的图像捕捉到视图层的应用程序。现在,当图像顶部的颜色为深色时,我希望状态栏变为白色,以优化用户体验。我正在考虑从特定像素获取颜色并测试该像素的颜色,然后我认为这不是一个好主意,因为可能只有该像素可能是不同的颜色。那么根据图像顶部的颜色更改状态栏颜色的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

检查您是否根据视图查看颜色并设置状态栏颜色,并在步骤下方更改状态栏颜色 转到你的app info.plist

1)将基于视图控制器的状态栏外观设置为NO

2)将状态栏样式设置为UIStatusBarStyleLightContent

然后设置使用以下代码

 UIView *topView=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
 topView.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:topView];

答案 1 :(得分:0)

当我最后遇到这种问题时 - 将可见文字放在任意图片的顶部 - 我们发现定义区域颜色的最有效+正确的方法只是将其缩放到一个像素并看到什么颜色是。现在可能是一个更好的方法,但是这个工作得很好我们从来没有得到任何抱怨,应该合理地直接适应你已经得到的任何图像数据缓冲格式:

+ (UIColor *)adaptiveColorWithBackgroundImage:(UIImage *)image forRegion:(CGRect)region
{
    region = CGRectMake(region.origin.x * image.scale,
                      region.origin.y * image.scale,
                      region.size.width * image.scale,
                      region.size.height * image.scale);

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, region);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(imageRef);

    return [UIColor adaptiveColorWithBackgroundImage:croppedImage];
}

+ (UIColor *)adaptiveColorWithBackgroundImage:(UIImage *)image
{
   unsigned char rgb[4] = { 0 };

   // trust scaling to come up with best colored pixel
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   CGContextRef bitmap = CGBitmapContextCreate(rgb,1,1,8,4,colorSpace,kCGImageAlphaNoneSkipLast);
    CGColorSpaceRelease(colorSpace);
   CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
   CGContextDrawImage(bitmap, CGRectMake(0,0,1,1), image.CGImage);
   CGContextRelease(bitmap);

   UIColor *imageColor = [UIColor colorWithRed:rgb[0]/255.f green:rgb[1]/255.f blue:rgb[2]/255.f alpha:1];
   UIColor *adaptiveColor = [UIColor adaptiveColorWithBackgroundColor:imageColor];

   return adaptiveColor;
}

+ (UIColor *)adaptiveColorWithBackgroundColor:(UIColor *)bkg
{
    CGFloat grayComponent = 0.5; //  this will default to dark text

    CGColorSpaceRef colorSpace = CGColorGetColorSpace(bkg.CGColor);
    CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);

    switch (colorSpaceModel)
    {
        case kCGColorSpaceModelLab:
        {
            // Fallthrough... treat the Luminance component the same as grayscale
        }
        case kCGColorSpaceModelMonochrome:
        {
            const CGFloat *components = CGColorGetComponents(bkg.CGColor);
            grayComponent = components[0];
            break;
        }   
        case kCGColorSpaceModelRGB: // convert to grayscale
        {
            const CGFloat *components = CGColorGetComponents(bkg.CGColor);
            grayComponent = (components[0] * 0.3f) + (components[1] * 0.59f) + (components[2] * 0.11f);
            break;
        }
        case kCGColorSpaceModelPattern:
        {
            unsigned char rgb[4] = { 0 };

            // trust scaling to come up with best colored pixel
            CGColorSpaceRef colorSpace1 = CGColorSpaceCreateDeviceRGB();
            CGContextRef bitmap = CGBitmapContextCreate(rgb,1,1,8,4,colorSpace1,kCGImageAlphaNoneSkipLast);
            CGColorSpaceRelease(colorSpace1);
            CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);

            CGContextSetFillColorWithColor(bitmap, [bkg CGColor]);
            CGContextFillRect(bitmap, CGRectMake(0,0,1,1));

            CGContextRelease(bitmap);

            grayComponent = (rgb[0]/255.0f * 0.3f) + (rgb[1]/255.0f * 0.59f) + (rgb[2]/255.0f * 0.11f);
            break;
        }
        default:
        {
            // unsuported colorspace return default
            break;
        }
    } 

    // dark background defined as greater than 60% gray
    // slightly biased towards white
    return (grayComponent <= 0.6) ? UIColor.whiteColor : UIColor.blackColor;   
}
相关问题