簡體   English   中英

如何根據視圖圖層的顏色更改狀態欄顏色

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

我有一個使用AVCaptureSession將相機上的圖像捕獲到視圖圖層的應用程序。 現在,當圖像頂部的顏色為深色時,我希望狀態欄變為白色,以優化用戶體驗。 我正在考慮從特定像素獲取顏色並測試該像素的顏色,然后我認為這不是一個好主意,因為可能只有該像素可以是其他顏色。 那么,根據圖像頂部的顏色更改狀態欄顏色的最佳方法是什么?

檢查您查看顏色並根據視圖設置狀態欄顏色,並在步驟Goto您的應用程序信息中使用更改狀態欄顏色。

1)將基於View控制器的狀態欄外觀設置為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];

當我上次遇到這種問題時-將可見的文本放在任意圖片的上方-我們發現定義區域顏色的最有效,最正確的方法就是將其縮放到一個像素,然后查看是什么顏色。 現在可能是一種更好的方法,但這已經足夠好了,我們再也沒有任何抱怨,應該可以合理地直接適應您使用的任何圖像數據緩沖區格式:

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