简体   繁体   English

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

[英]How to change status bar color according to the color of a view's layer

I have an app that uses AVCaptureSession to capture image on the camera to the view's layer. 我有一个使用AVCaptureSession将相机上的图像捕获到视图图层的应用程序。 Now I want the status bar to change to white color when the color on top of the image is a dark color just to optimize the user experience. 现在,当图像顶部的颜色为深色时,我希望状态栏变为白色,以优化用户体验。 I am thinking of getting the color from a specific pixel and test for the color of that pixel, then I think it is not a good idea because maybe only that pixel could be a different color. 我正在考虑从特定像素获取颜色并测试该像素的颜色,然后我认为这不是一个好主意,因为可能只有该像素可以是其他颜色。 So what is the best way to change the status bar color according to what is the color on the top of the image? 那么,根据图像顶部的颜色更改状态栏颜色的最佳方法是什么?

Check you view color and set status bar color according view and for change status bar color use below step Goto your app info.plist 检查您查看颜色并根据视图设置状态栏颜色,并在步骤Goto您的应用程序信息中使用更改状态栏颜色。

1) Set View controller-based status bar appearance to NO 1)将基于View控制器的状态栏外观设置为NO

2) Set Status bar style to UIStatusBarStyleLightContent 2)将状态栏样式设置为UIStatusBarStyleLightContent

Then set use below code 然后设置下面的代码使用

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

When I had this kind of problem last -- putting visible text on top of an arbitrary picture -- the most efficient+correct enough way we found to define a region's color was just scaling it to one pixel and seeing what color that was. 当我上次遇到这种问题时-将可见的文本放在任意图片的上方-我们发现定义区域颜色的最有效,最正确的方法就是将其缩放到一个像素,然后查看是什么颜色。 Might be a better way now, but this worked well enough we never got any complaints, should be reasonably straightforwardly adaptable to whatever image data buffer format you've got handy: 现在可能是一种更好的方法,但这已经足够好了,我们再也没有任何抱怨,应该可以合理地直接适应您使用的任何图像数据缓冲区格式:

+ (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;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM