iOS 实现app黑白模式

作者: 格雷s | 来源:发表于2020-05-09 10:50 被阅读0次

1问题思考

2020的是被疫情覆盖的一年,在国内疫情得到控制之后,全国在4.4日举行了疫情哀悼纪念,而互联网的产物在那天也做出了很多默契的配合,比如说很多网站暂停运营一天,很多app网页的UI变成了黑白模式,那天我刷了一整天的手机看到的画面全是黑白的,第二天又调整为正常了.作为一个iOS开发,我思考如何才能更好的达到这种黑白与彩色之前的切换,想了以下几种思路:

  • 1.iOS内置黑白开关
  • 2.动态配置,文字颜色和图片在当天通过服务端下发,修改成黑白色,这需要比较打的工作量
  • 3.UIColor相关方法捕获,图片滤镜

通过查询资料,未找到方式1相关的api,iOS设备自身有个功能叫颜色反转,有兴趣的可以去打开该功能看看,我在想这其实和实现黑白模式的原理是一样的,但是苹果未开放相关api

方式2看起来可行,我觉得一些app等在那天就是采用了这样的方式来实现的黑白化,因为app里的内容不是说有的都改成了黑白,只有部分图片或者页面改成了黑白模式,但是工作量有点太大了,而且不够灵活

方式3我认为是比较可靠可行的,于是我按照方式3的思路实现了UIColor及图片的黑白化

2功能实现

首先贴上一张没有开启黑白化的page原图,可以看到图片,color都是正常的


image.png

2.1UIColor黑白化

UIColor方法捕捉

#import "UIColor+Common.h"
#import <YLT_BaseLib/NSObject+YLT_Extension.h>
#import <Aspects.h>

//是否黑白化,1表示开启
#define monochromatic 1

@implementation UIColor (Common)

+ (void)load {
    //关键方法交换
    [UIColor ylt_swizzleClassMethod:@selector(gl_colorWithRed:green:blue:alpha:) withMethod:@selector(colorWithRed:green:blue:alpha:)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_blackColor) withMethod:@selector(blackColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_darkGrayColor) withMethod:@selector(darkGrayColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_lightGrayColor) withMethod:@selector(lightGrayColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_whiteColor) withMethod:@selector(whiteColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_grayColor) withMethod:@selector(grayColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_redColor) withMethod:@selector(redColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_greenColor) withMethod:@selector(greenColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_blueColor) withMethod:@selector(blueColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_cyanColor) withMethod:@selector(cyanColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_yellowColor) withMethod:@selector(yellowColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_magentaColor) withMethod:@selector(magentaColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_orangeColor) withMethod:@selector(orangeColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_purpleColor) withMethod:@selector(purpleColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_brownColor) withMethod:@selector(brownColor)];
    [UIColor ylt_swizzleClassMethod:@selector(gl_colorWithWhite:alpha:) withMethod:@selector(colorWithWhite:alpha:)];
    Class cls = NSClassFromString(@"UIDynamicSystemColor");
    [cls ylt_swizzleInstanceMethod:NSSelectorFromString(@"initWithName:colorsByThemeKey:") withMethod:@selector(gl_initWithName:colorsByThemeKey:)];
}

- (id)gl_initWithName:(id)name colorsByThemeKey:(id)key {
//    NSLog(@"name=%@  key =%@ ",name,key);
    if (monochromatic == 1) {
        if ([name isEqualToString:@"systemBlueColor"]) {
//                [UIColor systemBlueColor];
            return [UIColor blueColor];
        }
    }
   return [self gl_initWithName:name colorsByThemeKey:key];
}

+ (UIColor *)gl_colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
    //如果是单色模式(黑白模式),则平均r、g、b值
    if (monochromatic == 1) {
        //r,g,b权重调整,防止出现,1 0 0 、0 1 0,0 0 1同样的结果
        //0.2126,0.7152,0.0722 这三个是根据人眼对r,g,b三个颜色面感的强弱算出来的
        CGFloat brightness = (red * 0.2126 + 0.7152 * green + 0.0722 * blue);
//        CGFloat brightness = (red + green + blue) / 3.f;
        return [self gl_colorWithRed:brightness green:brightness blue:brightness alpha:alpha];
    }
    return [self gl_colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (UIColor *)gl_colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha {
    return [self colorWithRed:white green:white blue:white alpha:1];
}

+ (UIColor *)gl_blackColor {
    return [self colorWithWhite:0 alpha:1];
}

+ (UIColor *)gl_darkGrayColor {
    return [self colorWithWhite:0.333 alpha:1];
}

+ (UIColor *)gl_lightGrayColor {
    return [self colorWithWhite:0.667 alpha:1];
}

+ (UIColor *)gl_whiteColor {
    return [self colorWithWhite:1 alpha:1];
}

+ (UIColor *)gl_grayColor {
    return [self colorWithWhite:0.5 alpha:1];
}

+ (UIColor *)gl_redColor {
    return [self colorWithRed:1 green:0 blue:0 alpha:1];
}

+ (UIColor *)gl_greenColor {
    return [self colorWithRed:0 green:1 blue:0 alpha:1];
}
+ (UIColor *)gl_blueColor {
    return [self colorWithRed:0 green:0 blue:1 alpha:1];
}
+ (UIColor *)gl_cyanColor {
    return [self colorWithRed:0 green:1 blue:1 alpha:1];
}
+ (UIColor *)gl_yellowColor {
    return [self colorWithRed:1 green:1 blue:0 alpha:1];
}
+ (UIColor *)gl_magentaColor {
    return [self colorWithRed:1 green:0 blue:1 alpha:1];
}
+ (UIColor *)gl_orangeColor {
    return [self colorWithRed:1 green:0.5 blue:0 alpha:1];
}
+ (UIColor *)gl_purpleColor {
    return [self colorWithRed:0.5 green:0 blue:0.5 alpha:1];
}
+ (UIColor *)gl_brownColor {
    return [self colorWithRed:0.6 green:0.4 blue:0.2 alpha:1];
}

@end


我们注意以下这3处的颜色变化

image.png
  • 颜色1 导航栏的返回文字颜色的Class类型是属于UIDynamicSystemColor,其也属于UIColor的子类
    image.png
    捕获该类的实力方法initWithName:colorsByThemeKey:
[cls ylt_swizzleInstanceMethod:NSSelectorFromString(@"initWithName:colorsByThemeKey:") withMethod:@selector(gl_initWithName:colorsByThemeKey:)];

- (id)gl_initWithName:(id)name colorsByThemeKey:(id)key {
    NSLog(@"name=%@  key =%@ ",name,key);
    if (monochromatic == 1) {
        if ([name isEqualToString:@"systemBlueColor"]) {
            //    [UIColor systemBlueColor];
            return [UIColor blueColor];
        }
    }
   return [self gl_initWithName:name colorsByThemeKey:key];
}

在该方法中我们可以捕获到系统预设的systemColor,如果想知道到底有哪些systemColor,可以通过[UIColor systemBlueColor];点击前往分类@interface UIColor (UIColorSystemColors)中,这里系统提供了许多预设的systemColor,这些方法与initWithName:colorsByThemeKey:中的name一致,所以我们拦截了所有的systemBlueColor方法,直接采用BlueColor返回,而BlueColor我们在前面也做过了拦截处理,这样就达到了统一处理,如果还有其他name类型的systemColor,可以采取同样的处理方式,或者我们可以将需要捕获的systemColor的name通过服务端下发,同时下发对应的r、g、b、a值,然后调用[UIColor colorWithRed:r green:g blue:b alpha:a]这样更加灵活

  • 颜色2 和 颜色3都是通过自己去设置的颜色,自定义颜色方法比较有限,我总结并捕获了所有的Color类方法,即使有些第三方库识别16进制,但是最终调用的还是colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha,
    最终我们把所有颜色的拦截都流入到了下面swizzle method方法中
+ (UIColor *)gl_colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
    //如果是单色模式(黑白模式),则平均r、g、b值
    if (monochromatic == 1) {
        //r,g,b权重调整,防止出现,1 0 0 、0 1 0,0 0 1同样的结果
        //0.2126,0.7152,0.0722 这三个是根据人眼对r,g,b三个颜色面感的强弱算出来的
        CGFloat brightness = (red * 0.2126 + 0.7152 * green + 0.0722 * blue);
//        CGFloat brightness = (red + green + blue) / 3.f;
        return [self gl_colorWithRed:brightness green:brightness blue:brightness alpha:alpha];
    }
    return [self gl_colorWithRed:red green:green blue:blue alpha:alpha];
}

2.2UIImage黑白化

通常情况下,我们的app里的图片控件都会采用UIImageView,这里我只是正对UIImageView进行了setImage的hook,如果有其他情况,需要自己处理,掌握了此黑白化的技巧,其他的处理方式也差不多

//是否黑白化,1表示开启
#define monochromatic 1

@implementation UIImageView (Common)

+ (void)load {
    [UIImageView ylt_swizzleInstanceMethod:@selector(gl_setImage:) withMethod:@selector(setImage:)];
}

- (void)gl_setImage:(UIImage *)image {
    if (monochromatic == 1) {
        [self gl_setImage:[self gl_grayImage:image]];
    } else {
        [self gl_setImage:image];
    }
}

- (UIImage *)gl_grayImage:(UIImage *)image {
    //滤镜处理
    //CIPhotoEffectNoir黑白
    //CIPhotoEffectMono单色
    NSString *filterName = @"CIPhotoEffectNoir";
    CIFilter *filter = [CIFilter filterWithName:filterName];
    CIImage *inputImage = [[CIImage alloc] initWithImage:image];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    CGImageRef cgImage = [self.filterContext createCGImage:filter.outputImage fromRect:[inputImage extent]];
    UIImage *resultImg = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    return resultImg;
}

- (CIContext *)filterContext {
    CIContext *con = objc_getAssociatedObject(self, @selector(filterContext));
    if (!con) {
        con = [[CIContext alloc] initWithOptions:nil];
        self.filterContext = con;
    }
    return con;
}

- (void)setFilterContext:(CIContext *)filterContext {
    objc_setAssociatedObject(self, @selector(filterContext), filterContext, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

图片的黑白化,我采用的是CIFilter滤镜实现,我们看下黑白单色两种滤镜

image.png image.png

效果差不多,但是在图片色彩丰富的图片上,个人认为Mono会比Noir效果好看点

至此,我们可以通过服务器来灵活配置关键参数monochromatic,开启或者关闭黑白化功能,实现app基本全面黑白化
demo参考

相关文章

网友评论

    本文标题:iOS 实现app黑白模式

    本文链接:https://www.haomeiwen.com/subject/ymuhnhtx.html