+ (UIImage *)convertToGrayscale:(UIImage*)sourceImage { return [self convertToGrayscale:sourceImage.CGImage scale:sourceImage.scale]; } + (UIImage *)convertToGrayscale:(CGImageRef)sourceImage scale:(CGFloat)scale { if (!sourceImage) { return nil; } UIImage *resultUIImage = nil; @autoreleasepool { int width = (int)CGImageGetWidth(sourceImage); int height = (int)CGImageGetHeight(sourceImage); // the pixels will be painted to this array uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t)); // clear the pixels so any transparency is preserved memset(pixels, 0, width * height * sizeof(uint32_t)); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); // create a context with RGBA pixels CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); // paint the bitmap to our context which will fill in the pixels array CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage); for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x]; // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale uint8_t gray = (uint8_t)round(0.299 * rgbaPixel[1] + 0.587 * rgbaPixel[2] + 0.114 * rgbaPixel[3]); // set the pixels to gray rgbaPixel[1] = gray; rgbaPixel[2] = gray; rgbaPixel[3] = gray; } } // create a new CGImageRef from our context with the modified pixels CGImageRef image = CGBitmapContextCreateImage(context); // we're done with the context, color space, and pixels CGContextRelease(context); CGColorSpaceRelease(colorSpace); free(pixels); resultUIImage = [UIImage imageWithCGImage:image scale:scale orientation:UIImageOrientationUp]; // we're done with image now too CGImageRelease(image); } // make a new UIImage to return return resultUIImage; }
@interface CALayer (CustomerColorsLayer) @end @implementation CALayer (CustomerColorsLayer) + (void)load { // [self ms_swizzleMethod:@selector(setContents:) withMethod:@selector(mm_setContents:)]; // [self ms_swizzleMethod:@selector(drawInContext:) withMethod:@selector(mm_drawInContext:)]; } - (void)mm_setContents:(id)contents { static CFTypeID imageType = 0; if (imageType == 0) { imageType = CFGetTypeID([UIImage imageWithColor:[UIColor whiteColor]].CGImage); } if (contents != NULL && CFGetTypeID((__bridge CFTypeRef)(contents)) == imageType) { @autoreleasepool { UIImage *sourceImage = [UIImage imageWithCGImage:(__bridge CGImageRef _Nonnull)(contents)]; UIImage *grayImage = [UIImage convertToGrayscale:sourceImage]; [self mm_setContents:(__bridge id)grayImage.CGImage]; } } else { [self mm_setContents:contents]; } } - (void)mm_drawInContext:(CGContextRef)ctx { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); CGContextSetFillColorSpace(ctx, colorSpace); [self mm_drawInContext:ctx]; CGColorSpaceRelease(colorSpace); } @end @interface UIImageView (CustomerColorsLayer) @end @implementation UIImageView (CustomerColorsLayer) + (void)load { [self ms_swizzleMethod:@selector(setImage:) withMethod:@selector(mm_setImage:)]; } - (void)mm_setImage:(UIImage *)image { [self mm_setImage:[UIImage convertToGrayscale:image]]; } @end @interface UIColor (CustomerColorsLayer) @end @implementation UIColor (CustomerColorsLayer) + (void)load { [self ms_swizzleClassMethod:@selector(colorWithRed:green:blue:alpha:) withMethod:@selector(grayColorWithRed:green:blue:alpha:)]; } + (UIColor *)grayColorWithRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a { CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114; UIColor *grayColor = [UIColor grayColorWithRed:gray green:gray blue:gray alpha:a]; return grayColor; } @end