[英]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.