├── README.md └── Category-分类 ├── UIImageView+GrayImageview.h ├── UIImage+GrayImage.h ├── UIButton+LhGray.h ├── UILabel+LhGray.h ├── UIColor+LhGray.h ├── WKWebView+LhGray.h ├── UIPageControl+LhGray.h ├── UITabBarItem+LhGray.h ├── UINavigationBar+LhGray.h ├── UIImageView+GrayImageview.m ├── UINavigationBar+LhGray.m ├── UIPageControl+LhGray.m ├── UILabel+LhGray.m ├── UIButton+LhGray.m ├── WKWebView+LhGray.m ├── UITabBarItem+LhGray.m ├── UIImage+GrayImage.m └── UIColor+LhGray.m /README.md: -------------------------------------------------------------------------------- 1 | 悼念日模式 2 | app 整体风格变灰色 -------------------------------------------------------------------------------- /Category-分类/UIImageView+GrayImageview.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIWebView+Gray.h 3 | // 4 | // 5 | // 6 | 7 | #import 8 | 9 | NS_ASSUME_NONNULL_BEGIN 10 | 11 | @interface UIImageView (GrayImageview) 12 | 13 | +(void)lh_imageViewSwizzldMethedWith:(BOOL)changeGray; 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /Category-分类/UIImage+GrayImage.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIWebView+Gray.h 3 | // 4 | // 5 | // 6 | 7 | #import 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface UIImage (GrayImage) 13 | 14 | //转化灰度 15 | - (UIImage *)grayImage; 16 | 17 | +(void)lh_imageSwizzldMethedWith:(BOOL)changeGray; 18 | 19 | @end 20 | 21 | NS_ASSUME_NONNULL_END 22 | -------------------------------------------------------------------------------- /Category-分类/UIButton+LhGray.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIButton+LhGray.h 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/26. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UIButton (LhGray) 14 | +(void)lh_buttonSwizzldMethedWith:(BOOL)changeGray; 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /Category-分类/UILabel+LhGray.h: -------------------------------------------------------------------------------- 1 | // 2 | // UILabel+LhGray.h 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/26. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UILabel (LhGray) 14 | 15 | +(void)lh_lableSwizzldMethedWith:(BOOL)changeGray; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Category-分类/UIColor+LhGray.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+LhGray.h 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UIColor (LhGray) 14 | 15 | +(void)lh_colorSwizzldColorMethedWith:(BOOL)changeGray; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Category-分类/WKWebView+LhGray.h: -------------------------------------------------------------------------------- 1 | // 2 | // WKWebView+LhGray.h 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface WKWebView (LhGray) 14 | 15 | +(void)lh_WKWebViewWizzldMethedWith:(BOOL)changeGray; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Category-分类/UIPageControl+LhGray.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIPageControl+LhGray.h 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UIPageControl (LhGray) 14 | 15 | +(void)lh_pageControlSwizzldMethedWith:(BOOL)changeGray; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Category-分类/UITabBarItem+LhGray.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITabBarItem+LhGray.h 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UITabBarItem (LhGray) 14 | 15 | +(void)lh_tabbarItemSwizzldMethedWith:(BOOL)changeGray; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Category-分类/UINavigationBar+LhGray.h: -------------------------------------------------------------------------------- 1 | // 2 | // UINavigationBar+LhGray.h 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UINavigationBar (LhGray) 14 | 15 | +(void)lh_navigationBarSwizzldMethedWith:(BOOL)changeGray; 16 | @end 17 | 18 | NS_ASSUME_NONNULL_END 19 | -------------------------------------------------------------------------------- /Category-分类/UIImageView+GrayImageview.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+GrayColor.m 3 | // 4 | // 5 | // 6 | 7 | #import "UIImageView+GrayImageview.h" 8 | #import "UIImage+GrayImage.h" 9 | #import 10 | 11 | @implementation UIImageView (GrayImageview) 12 | 13 | +(void)lh_imageViewSwizzldMethedWith:(BOOL)changeGray;{ 14 | 15 | if (changeGray == false) { 16 | return; 17 | } 18 | 19 | Class class = [self class]; 20 | 21 | SEL originalSelector = @selector(setImage:); 22 | SEL swizzledSelector = @selector(swizzled_setImage:); 23 | 24 | Method originalMethod = class_getInstanceMethod(class, originalSelector); 25 | Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); 26 | BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); 27 | if (didAddMethod) { 28 | class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); 29 | } else { 30 | method_exchangeImplementations(originalMethod, swizzledMethod); 31 | } 32 | } 33 | 34 | 35 | - (void)swizzled_setImage:(UIImage *)image { 36 | //系统键盘处理(如果不过滤,这系统键盘字母背景是黑色) 37 | if ([self.superview isKindOfClass:NSClassFromString(@"UIKBSplitImageView")]) { 38 | [self swizzled_setImage:image]; 39 | return; 40 | } 41 | UIImage *im = [[image grayImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 42 | // UIImage *im = [image grayImage]; 43 | [self swizzled_setImage:im]; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /Category-分类/UINavigationBar+LhGray.m: -------------------------------------------------------------------------------- 1 | // 2 | // UINavigationBar+LhGray.m 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import "UINavigationBar+LhGray.h" 10 | #import 11 | #import "UIImage+GrayImage.h" 12 | 13 | @implementation UINavigationBar (LhGray) 14 | +(void)lh_navigationBarSwizzldMethedWith:(BOOL)changeGray{ 15 | if (changeGray == false) { 16 | return; 17 | } 18 | Class cls = [self class]; 19 | 20 | SEL oriSel = @selector(setBarTintColor:); 21 | SEL swizzldSel = @selector(lh_setNavBarTintColor:); 22 | 23 | Method originMethed = class_getInstanceMethod(cls, oriSel); 24 | Method swizzldMethed = class_getInstanceMethod(cls, swizzldSel); 25 | 26 | BOOL isAddMethed = class_addMethod(cls, oriSel, method_getImplementation(swizzldMethed),method_getTypeEncoding(swizzldMethed)); 27 | if (isAddMethed) { 28 | class_replaceMethod(cls, swizzldSel, method_getImplementation(originMethed), method_getTypeEncoding(originMethed)); 29 | }else{ 30 | method_exchangeImplementations(originMethed,swizzldMethed); 31 | } 32 | 33 | } 34 | 35 | - (void)lh_setNavBarTintColor:(UIColor *)color{ 36 | UIColor * newColor = [self changeGrayWithColor:color Red:1.0 green:0.0 blue:0.0 alpha:1.0]; 37 | [self lh_setNavBarTintColor:newColor]; 38 | 39 | } 40 | 41 | - (UIColor *)changeGrayWithColor:(UIColor *)color Red:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a { 42 | CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114; 43 | UIColor *grayColor = [UIColor colorWithWhite:gray alpha:a]; 44 | return grayColor; 45 | } 46 | 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /Category-分类/UIPageControl+LhGray.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIPageControl+LhGray.m 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import "UIPageControl+LhGray.h" 10 | #import 11 | 12 | @implementation UIPageControl (LhGray) 13 | 14 | +(void)lh_pageControlSwizzldMethedWith:(BOOL)changeGray{ 15 | if (changeGray == false) { 16 | return; 17 | } 18 | Class cls = [self class]; 19 | 20 | SEL oriSel = @selector(setCurrentPageIndicatorTintColor:); 21 | SEL swizzldSel = @selector(lh_setCurrentPageIndicatorTintColor:); 22 | 23 | Method originMethed = class_getInstanceMethod(cls, oriSel); 24 | Method swizzldMethed = class_getInstanceMethod(cls, swizzldSel); 25 | 26 | BOOL isAddMethed = class_addMethod(cls, oriSel, method_getImplementation(swizzldMethed),method_getTypeEncoding(swizzldMethed)); 27 | if (isAddMethed) { 28 | class_replaceMethod(cls, swizzldSel, method_getImplementation(originMethed), method_getTypeEncoding(originMethed)); 29 | }else{ 30 | method_exchangeImplementations(originMethed,swizzldMethed); 31 | } 32 | } 33 | -(void)lh_setCurrentPageIndicatorTintColor:(UIColor *)color{ 34 | UIColor * newColor = [self changeGrayWithColor:color Red:1.0 green:0.0 blue:0.0 alpha:1.0]; 35 | [self lh_setCurrentPageIndicatorTintColor:newColor]; 36 | 37 | } 38 | 39 | - (UIColor *)changeGrayWithColor:(UIColor *)color Red:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a { 40 | CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114; 41 | UIColor *grayColor = [UIColor colorWithWhite:gray alpha:a]; 42 | return grayColor; 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /Category-分类/UILabel+LhGray.m: -------------------------------------------------------------------------------- 1 | // 2 | // UILabel+LhGray.m 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/26. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import "UILabel+LhGray.h" 10 | #import 11 | 12 | @implementation UILabel (LhGray) 13 | 14 | +(void)lh_lableSwizzldMethedWith:(BOOL)changeGray{ 15 | 16 | if (changeGray == false) { 17 | return; 18 | } 19 | 20 | Class class = [self class]; 21 | 22 | SEL originalSelector = @selector(setTextColor:); 23 | SEL swizzledSelector = @selector(lh_setLableTextColor:); 24 | 25 | Method originalMethod = class_getInstanceMethod(class, originalSelector); 26 | Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); 27 | BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); 28 | if (didAddMethod) { 29 | class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); 30 | } else { 31 | method_exchangeImplementations(originalMethod, swizzledMethod); 32 | } 33 | } 34 | 35 | 36 | - (void)lh_setLableTextColor:(UIColor *)color { 37 | UIColor * newColor = [self changeGrayWithColor:[UIColor redColor] Red:1.0 green:0.0 blue:0.0 alpha:1.0]; 38 | [self lh_setLableTextColor:newColor]; 39 | 40 | } 41 | - (UIColor *)changeGrayWithColor:(UIColor *)color Red:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a { 42 | CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114; 43 | UIColor *grayColor = [UIColor colorWithWhite:gray alpha:a]; 44 | return grayColor; 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /Category-分类/UIButton+LhGray.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIButton+LhGray.m 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/26. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import "UIButton+LhGray.h" 10 | #import 11 | 12 | @implementation UIButton (LhGray) 13 | +(void)lh_buttonSwizzldMethedWith:(BOOL)changeGray{ 14 | if (changeGray == false) { 15 | return; 16 | } 17 | 18 | Class class = [self class]; 19 | 20 | SEL originalSelector = @selector(setBackgroundColor:); 21 | SEL swizzledSelector = @selector(lh_setButtonBackgroundColor:); 22 | 23 | Method originalMethod = class_getInstanceMethod(class, originalSelector); 24 | Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); 25 | BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); 26 | if (didAddMethod) { 27 | class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); 28 | } else { 29 | method_exchangeImplementations(originalMethod, swizzledMethod); 30 | } 31 | } 32 | 33 | 34 | - (void)lh_setButtonBackgroundColor:(UIColor *)color { 35 | UIColor * newColor = [self changeGrayWithColor:[UIColor redColor] Red:1.0 green:0.0 blue:0.0 alpha:1.0]; 36 | [self lh_setButtonBackgroundColor:newColor]; 37 | 38 | } 39 | - (UIColor *)changeGrayWithColor:(UIColor *)color Red:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a { 40 | CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114; 41 | UIColor *grayColor = [UIColor colorWithWhite:gray alpha:a]; 42 | return grayColor; 43 | } 44 | @end 45 | -------------------------------------------------------------------------------- /Category-分类/WKWebView+LhGray.m: -------------------------------------------------------------------------------- 1 | // 2 | // WKWebView+LhGray.m 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import "WKWebView+LhGray.h" 10 | #import 11 | #import "SCConst.h" 12 | 13 | @implementation WKWebView (LhGray) 14 | 15 | +(void)lh_WKWebViewWizzldMethedWith:(BOOL)changeGray{ 16 | if (changeGray == false) { 17 | return; 18 | } 19 | 20 | Method originalMethod = class_getInstanceMethod([self class], @selector(initWithFrame:configuration:)); 21 | Method swizzledMethod = class_getInstanceMethod([self class], @selector(lg_initWithFrame:configuration:)); 22 | method_exchangeImplementations(originalMethod, swizzledMethod); 23 | } 24 | 25 | 26 | - (instancetype)lg_initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration { 27 | // js脚本 28 | NSString *jScript = @"var filter = '-webkit-filter:grayscale(100%);-moz-filter:grayscale(100%); -ms-filter:grayscale(100%); -o-filter:grayscale(100%) filter:grayscale(100%);';document.getElementsByTagName('html')[0].style.filter = 'grayscale(100%)';"; 29 | // 注入 30 | WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; 31 | 32 | WKUserContentController *userController = [[WKUserContentController alloc] init]; 33 | [userController addUserScript:wkUScript]; 34 | 35 | // 配置对象 36 | WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; 37 | wkWebConfig.userContentController = userController; 38 | configuration = wkWebConfig; 39 | WKWebView *webView = [self lg_initWithFrame:frame configuration:configuration]; 40 | return webView; 41 | } 42 | 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /Category-分类/UITabBarItem+LhGray.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITabBarItem+LhGray.m 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import "UITabBarItem+LhGray.h" 10 | #import 11 | 12 | @implementation UITabBarItem (LhGray) 13 | + (void)lh_tabbarItemSwizzldMethedWith:(BOOL)changeGray{ 14 | if (changeGray == false) { 15 | return; 16 | } 17 | Class cls = [self class]; 18 | 19 | SEL oriSel = @selector(setSelectedImage:); 20 | SEL swizzldSel = @selector(lh_setTabbarItemImage:); 21 | 22 | Method originMethed = class_getInstanceMethod(cls, oriSel); 23 | Method swizzldMethed = class_getInstanceMethod(cls, swizzldSel); 24 | 25 | BOOL isAddMethed = class_addMethod(cls, oriSel, method_getImplementation(swizzldMethed),method_getTypeEncoding(swizzldMethed)); 26 | if (isAddMethed) { 27 | class_replaceMethod(cls, swizzldSel, method_getImplementation(originMethed), method_getTypeEncoding(originMethed)); 28 | }else{ 29 | method_exchangeImplementations(originMethed,swizzldMethed); 30 | } 31 | } 32 | - (void)lh_setTabbarItemImage:(UIImage *)image { 33 | UIImage *im = [[self imageToTransparent:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 34 | [self lh_setTabbarItemImage:im]; 35 | } 36 | 37 | - (UIImage*)imageToTransparent:(UIImage*) image 38 | { 39 | // 分配内存 40 | const int imageWidth = image.size.width; 41 | const int imageHeight = image.size.height; 42 | size_t bytesPerRow = imageWidth * 4; 43 | uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight); 44 | 45 | // 创建context 46 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 47 | CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace, 48 | kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); 49 | CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage); 50 | 51 | // 遍历像素 52 | int pixelNum = imageWidth * imageHeight; 53 | uint32_t* pCurPtr = rgbImageBuf; 54 | for (int i = 0; i < pixelNum; i++, pCurPtr++) 55 | { 56 | uint8_t* ptr = (uint8_t*)pCurPtr; 57 | if (ptr[3] < 50 && ptr[3] < 50 && ptr[1] < 50) { 58 | ptr[0] = 0; 59 | } else { 60 | // 灰度算法 61 | uint8_t gray = ptr[3] * 0.299 + ptr[2] * 0.587 + ptr[1] * 0.114; 62 | ptr[3] = gray; //0~255 63 | ptr[2] = gray; 64 | ptr[1] = gray; 65 | } 66 | } 67 | 68 | // 将内存转成image 69 | CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData); 70 | 71 | CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace, 72 | kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider, 73 | NULL, true,kCGRenderingIntentDefault); 74 | CGDataProviderRelease(dataProvider); 75 | 76 | UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef]; 77 | 78 | // 释放 79 | CGImageRelease(imageRef); 80 | CGContextRelease(context); 81 | CGColorSpaceRelease(colorSpace); 82 | 83 | return resultUIImage; 84 | } 85 | 86 | void ProviderReleaseData (void *info, const void *data, size_t size) 87 | { 88 | free((void*)data); 89 | } 90 | 91 | @end 92 | -------------------------------------------------------------------------------- /Category-分类/UIImage+GrayImage.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+GrayColor.m 3 | // 4 | // 5 | // 6 | 7 | #import "UIImage+GrayImage.h" 8 | 9 | @implementation UIImage (GrayImage) 10 | 11 | +(void)lh_imageSwizzldMethedWith:(BOOL)changeGray{ 12 | 13 | if (changeGray == false) { 14 | return; 15 | } 16 | //交换方法 17 | Class cls = object_getClass(self); 18 | Method originMethod = class_getClassMethod(cls, @selector(imageNamed:)); 19 | Method swizzledMethod = class_getClassMethod(cls, @selector(lg_imageNamed:)); 20 | 21 | [self swizzleMethodWithOriginSel:@selector(imageNamed:) oriMethod:originMethod swizzledSel:@selector(lg_imageNamed:) swizzledMethod:swizzledMethod class:cls]; 22 | 23 | Method originMethod1 = class_getClassMethod(cls, @selector(imageWithData:)); 24 | Method swizzledMethod1 = class_getClassMethod(cls, @selector(lg_imageWithData:)); 25 | [self swizzleMethodWithOriginSel:@selector(imageWithData:) oriMethod:originMethod1 swizzledSel:@selector(lg_imageWithData:) swizzledMethod:swizzledMethod1 class:cls]; 26 | 27 | } 28 | 29 | + (void)swizzleMethodWithOriginSel:(SEL)oriSel 30 | oriMethod:(Method)oriMethod 31 | swizzledSel:(SEL)swizzledSel 32 | swizzledMethod:(Method)swizzledMethod 33 | class:(Class)cls { 34 | BOOL didAddMethod = class_addMethod(cls, oriSel, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); 35 | 36 | if (didAddMethod) { 37 | class_replaceMethod(cls, swizzledSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod)); 38 | } else { 39 | method_exchangeImplementations(oriMethod, swizzledMethod); 40 | } 41 | } 42 | 43 | + (UIImage *)lg_imageWithData:(NSData *)data { 44 | UIImage *image = [self lg_imageWithData:data]; 45 | return [image grayImage]; 46 | } 47 | 48 | + (UIImage *)lg_imageNamed:(NSString *)name { 49 | UIImage *image = [self lg_imageNamed:name]; 50 | return [image grayImage]; 51 | } 52 | 53 | // 转化灰度 54 | - (UIImage *)grayImage { 55 | const int RED =1; 56 | const int GREEN =2; 57 | const int BLUE =3; 58 | 59 | // Create image rectangle with current image width/height 60 | CGRect imageRect = CGRectMake(0,0, self.size.width* self.scale, self.size.height* self.scale); 61 | 62 | int width = imageRect.size.width; 63 | int height = imageRect.size.height; 64 | 65 | // the pixels will be painted to this array 66 | uint32_t *pixels = (uint32_t*) malloc(width * height *sizeof(uint32_t)); 67 | 68 | // clear the pixels so any transparency is preserved 69 | memset(pixels,0, width * height *sizeof(uint32_t)); 70 | 71 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 72 | 73 | // create a context with RGBA pixels 74 | CGContextRef context = CGBitmapContextCreate(pixels, width, height,8, width *sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); 75 | 76 | // paint the bitmap to our context which will fill in the pixels array 77 | CGContextDrawImage(context,CGRectMake(0,0, width, height), [self CGImage]); 78 | 79 | for(int y = 0; y < height; y++) { 80 | for(int x = 0; x < width; x++) { 81 | uint8_t *rgbaPixel = (uint8_t*) &pixels[y * width + x]; 82 | 83 | // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale 84 | uint32_t gray = 0.3 * rgbaPixel[RED] +0.59 * rgbaPixel[GREEN] +0.11 * rgbaPixel[BLUE]; 85 | 86 | // set the pixels to gray 87 | rgbaPixel[RED] = gray; 88 | rgbaPixel[GREEN] = gray; 89 | rgbaPixel[BLUE] = gray; 90 | } 91 | } 92 | 93 | // create a new CGImageRef from our context with the modified pixels 94 | CGImageRef imageRef = CGBitmapContextCreateImage(context); 95 | 96 | // we're done with the context, color space, and pixels 97 | CGContextRelease(context); 98 | CGColorSpaceRelease(colorSpace); 99 | free(pixels); 100 | 101 | // make a new UIImage to return 102 | UIImage *resultUIImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:UIImageOrientationUp]; 103 | 104 | // we're done with image now too 105 | CGImageRelease(imageRef); 106 | 107 | return resultUIImage; 108 | } 109 | 110 | 111 | @end 112 | -------------------------------------------------------------------------------- /Category-分类/UIColor+LhGray.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+LhGray.m 3 | // SmartCity 4 | // 5 | // Created by luoh on 2021/3/25. 6 | // Copyright © 2021 StarHome. All rights reserved. 7 | // 8 | 9 | #import "UIColor+LhGray.h" 10 | #import 11 | 12 | @implementation UIColor (LhGray) 13 | 14 | +(void)lh_colorSwizzldColorMethedWith:(BOOL)changeGray{ 15 | 16 | Class cls = object_getClass(self); 17 | 18 | //将系统提供的colorWithRed:green:blue:alpha:替换掉 19 | Method originMethod = class_getClassMethod(cls, @selector(colorWithRed:green:blue:alpha:)); 20 | Method swizzledMethod = class_getClassMethod(cls, @selector(lg_colorWithRed:green:blue:alpha:)); 21 | [self swizzleMethodWithOriginSel:@selector(colorWithRed:green:blue:alpha:) oriMethod:originMethod swizzledSel:@selector(lg_colorWithRed:green:blue:alpha:) swizzledMethod:swizzledMethod class:cls]; 22 | 23 | //将系统提供的colors也替换掉 24 | NSArray *array = [NSArray arrayWithObjects:@"redColor",@"greenColor",@"blueColor",@"cyanColor",@"yellowColor",@"magentaColor",@"orangeColor",@"purpleColor",@"brownColor",@"systemBlueColor",@"systemGreenColor", nil]; 25 | 26 | for (int i = 0; i < array.count ; i ++) { 27 | SEL sel = NSSelectorFromString(array[i]); 28 | SEL lg_sel = NSSelectorFromString([NSString stringWithFormat:@"lg_%@",array[i]]); 29 | Method originMethod = class_getClassMethod(cls, sel); 30 | Method swizzledMethod = class_getClassMethod(cls, lg_sel); 31 | [self swizzleMethodWithOriginSel:sel oriMethod:originMethod swizzledSel:lg_sel swizzledMethod:swizzledMethod class:cls]; 32 | } 33 | } 34 | 35 | + (void)swizzleMethodWithOriginSel:(SEL)oriSel 36 | oriMethod:(Method)oriMethod 37 | swizzledSel:(SEL)swizzledSel 38 | swizzledMethod:(Method)swizzledMethod 39 | class:(Class)cls { 40 | BOOL didAddMethod = class_addMethod(cls, oriSel, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); 41 | 42 | if (didAddMethod) { 43 | class_replaceMethod(cls, swizzledSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod)); 44 | } else { 45 | method_exchangeImplementations(oriMethod, swizzledMethod); 46 | } 47 | } 48 | 49 | + (UIColor *)lg_redColor { 50 | // 1.0, 0.0, 0.0 RGB 51 | UIColor *color = [self lg_redColor]; 52 | color = [self changeGrayWithColor:color Red:1.0 green:0.0 blue:0.0 alpha:1.0]; 53 | return color; 54 | } 55 | 56 | + (UIColor *)lg_greenColor { 57 | // 0.0, 1.0, 0.0 RGB 58 | UIColor *color = [self lg_greenColor]; 59 | color = [self changeGrayWithColor:color Red:0.0 green:1.0 blue:0.0 alpha:1.0]; 60 | return color; 61 | } 62 | 63 | + (UIColor *)lg_blueColor { 64 | //0.0, 0.0, 1.0 65 | UIColor *color = [self lg_blueColor]; 66 | color = [self changeGrayWithColor:color Red:0.0 green:0.0 blue:1.0 alpha:1.0]; 67 | return color; 68 | } 69 | 70 | + (UIColor *)lg_cyanColor { 71 | // 0.0, 1.0, 1.0 72 | UIColor *color = [self lg_cyanColor]; 73 | color = [self changeGrayWithColor:color Red:0.0 green:1.0 blue:1.0 alpha:1.0]; 74 | return color; 75 | } 76 | 77 | + (UIColor *)lg_yellowColor { 78 | //1.0, 1.0, 0.0 79 | UIColor *color = [self lg_yellowColor]; 80 | color = [self changeGrayWithColor:color Red:1.0 green:1.0 blue:0.0 alpha:1.0]; 81 | return color; 82 | } 83 | 84 | + (UIColor *)lg_magentaColor { 85 | // 1.0, 0.0, 1.0 86 | UIColor *color = [self lg_magentaColor]; 87 | color = [self changeGrayWithColor:color Red:1.0 green:0.0 blue:1.0 alpha:1.0]; 88 | return color; 89 | } 90 | 91 | + (UIColor *)lg_orangeColor { 92 | // 1.0, 0.5, 0.0 93 | UIColor *color = [self lg_orangeColor]; 94 | color = [self changeGrayWithColor:color Red:1.0 green:0.5 blue:0.0 alpha:1.0]; 95 | return color; 96 | } 97 | 98 | + (UIColor *)lg_systemBlueColor { 99 | UIColor *color = [self lg_systemBlueColor]; 100 | color = [self changeGrayWithColor:color Red:0.0 green:0.0 blue:1.0 alpha:1.0]; 101 | return color; 102 | } 103 | 104 | + (UIColor *)lg_systemGreenColor { 105 | UIColor *color = [self lg_systemGreenColor]; 106 | color = [self changeGrayWithColor:color Red:0.0 green:1.0 blue:0.0 alpha:1.0]; 107 | return color; 108 | } 109 | 110 | + (UIColor *)lg_purpleColor { 111 | // 0.5, 0.0, 0.5 112 | UIColor *color = [self lg_purpleColor]; 113 | color = [self changeGrayWithColor:color Red:0.5 green:0.0 blue:0.5 alpha:1.0]; 114 | return color; 115 | } 116 | 117 | + (UIColor *)lg_brownColor { 118 | // 0.6, 0.4, 0.2 119 | UIColor *color = [self lg_brownColor]; 120 | color = [self changeGrayWithColor:color Red:0.6 green:0.4 blue:0.2 alpha:1.0]; 121 | return color; 122 | } 123 | 124 | + (instancetype)lg_colorWithRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a { 125 | 126 | UIColor *color = [self lg_colorWithRed:r green:g blue:b alpha:a]; 127 | if (r == 0 && g == 0 && b == 0) { 128 | return color; 129 | } 130 | color = [self changeGrayWithColor:color Red:r green:g blue:b alpha:a]; 131 | return color; 132 | } 133 | 134 | + (UIColor *)changeGrayWithColor:(UIColor *)color Red:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a { 135 | CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114; 136 | UIColor *grayColor = [UIColor colorWithWhite:gray alpha:a]; 137 | return grayColor; 138 | } 139 | 140 | @end 141 | --------------------------------------------------------------------------------