Advertisement
rehannali

Change image color objective c

Jun 28th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (UIImage *)image:(UIImage *)image WithTint:(UIColor *)tintColor
  2. {
  3.     // Begin drawing
  4.     CGRect aRect = CGRectMake(0.f, 0.f, image.size.width, image.size.height);
  5.     CGImageRef alphaMask;
  6.    
  7.     //
  8.     // Compute mask flipping image
  9.     //
  10.     {
  11.         UIGraphicsBeginImageContext(aRect.size);
  12.         CGContextRef c = UIGraphicsGetCurrentContext();
  13.        
  14.         // draw image
  15.         CGContextTranslateCTM(c, 0, aRect.size.height);
  16.         CGContextScaleCTM(c, 1.0, -1.0);
  17.         [image drawInRect: aRect];
  18.        
  19.         alphaMask = CGBitmapContextCreateImage(c);
  20.        
  21.         UIGraphicsEndImageContext();
  22.     }
  23.    
  24.     //
  25.     UIGraphicsBeginImageContext(aRect.size);
  26.    
  27.     // Get the graphic context
  28.     CGContextRef c = UIGraphicsGetCurrentContext();
  29.    
  30.     // Draw the image
  31.     [image drawInRect:aRect];
  32.    
  33.     // Mask
  34.     CGContextClipToMask(c, aRect, alphaMask);
  35.    
  36.     // Set the fill color space
  37.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  38.     CGContextSetFillColorSpace(c, colorSpace);
  39.    
  40.     // Set the fill color
  41.     CGContextSetFillColorWithColor(c, tintColor.CGColor);
  42.    
  43.     UIRectFillUsingBlendMode(aRect, kCGBlendModeNormal);
  44.    
  45.     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  46.     UIGraphicsEndImageContext();
  47.    
  48.     // Release memory
  49.     CGColorSpaceRelease(colorSpace);
  50.     CGImageRelease(alphaMask);
  51.    
  52.     return img;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement