Handling source: www.jianshu.com/p/3db816b46…

Author: Left

For those interested in iOS development, check out the author’s iOS chat group:812157648In the group, you can blow water and exchange relevant knowledge. There are also some materials about the interview that I have sorted out. Welcome to join the group and drive together

The App needs to be completely colored during the national mourning day. The implementation is as follows. So let’s create a UIImageView subclass

I’m going to rewrite setImage.

- (void)setImage:(UIImage *)image
{
    super.image = [self makeGrayImage:image];
}

Copy the code

Look for everything in Xcode that uses UIImageView, and replace it with whatever subclass you created. (Remember not to select the Pods section to avoid too much modification.) This way SDWebImage will also use setImage to display images. It turns all the pictures gray. (Button, etc., can do the same).

1. You can modify the image saturation

- (UIImage *) makeGrayImage: (image UIImage *) {/ / modify the saturation of 0 CIImage * beginImage = [CIImage imageWithCGImage: image. CGImage]; CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"]; [filter setValue:beginImage forKey:kCIInputImageKey]; // Saturation 0-- 2 Default to 1 [filter setValue:0 forKey:@"inputSaturation"]; CIImage *outputImage = [filter outputImage]; CIContext *context = [CIContext contextWithOptions: nil]; CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; UIImage *newImg = [UIImage imageWithCGImage:cgimg]; // Release the C object CGImageRelease(cgimg); return newImg; }Copy the code

Advantages: Saturation of the process of 0 is the process of the picture color. It shows the picture perfectly as it should be. Disadvantages: Large memory usage.

2. Can be modified by gray processing method

- (UIImage*)systemImageToGrayImage:(UIImage*)image{ int width = image.size.width; int height = image.size.height; / / the first step: create color space (ie, open up a color memory space) / / image gray processing (create gray space) CGColorSpaceRef colorRef = CGColorSpaceCreateDeviceGray (); // Parameter 1: memory size (address pointing to this memory area)(memory address) // Parameter 2: image width // Parameter 3: image height // Parameter 4: pixel number (color space, e.g. : 32-bit pixel format and RGB color space,8 bits) CGContextRef Context = CGBitmapContextCreate(nil, width, height, 8, 0, colorRef, kCGImageAlphaNone); // Free memory CGColorSpaceRelease(colorRef); if (context == nil) { return nil; // Parameter 1: context // Parameter 2: render area // parameter 3: source file (original image) CGContextDrawImage(context, CGRectMake(0, 0, 0) width, height), image.CGImage); / / the fourth step: paint color space into a CGImage (into identifiable picture type) CGImageRef grayImageRef = CGBitmapContextCreateImage (context); / / step 5: to transfer the picture of the C/C + + CGImage object-oriented UIImage (picture type) into the iOS program know UIImage * dstImage = [UIImage imageWithCGImage: grayImageRef]; // Release memory CGContextRelease(context); CGImageRelease(grayImageRef); return dstImage; }Copy the code

Advantages: After several tests, the memory footprint is 5-10MB lower than the modified saturation method.

Disadvantages: PNG transparent parts can turn black (fine if you don’t do anything to controls with transparent parts).