title: How can I tint a UIImage with gradient?
body: <p>I searched everywhere but didn't find the solution. I have image 1. How can I programatically tint them with gradient to get images 2 and 3? Here are those images:</p>
  
  <p><img src="https://i.stack.imgur.com/dUUgS.png" alt="images 1, 2, 3"></p>
  
  <p>Tints that I applied to them via Photoshop are simple 2-color linear gradients.</p>
  
  <p>And my question is: how can I achieve this effect programatically?</p>
  
  <hr>
  
  <p><strong>Solution:</strong> jrtc27 gave me <em>almost</em> working example. I fixed it (for ARC) and made it reusable (using UIImage's category). Here is it:</p>
  
  <pre><code>- (UIImage *)tintedWithLinearGradientColors:(NSArray *)colorsArr {
      CGFloat scale = self.scale;
      UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale, self.size.height * scale));
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextTranslateCTM(context, 0, self.size.height);
      CGContextScaleCTM(context, 1.0, -1.0);
  
      CGContextSetBlendMode(context, kCGBlendModeNormal);
      CGRect rect = CGRectMake(0, 0, self.size.width * scale, self.size.height * scale);
      CGContextDrawImage(context, rect, self.CGImage);
  
      // Create gradient
  
      UIColor *colorOne = [colorsArr objectAtIndex:1]; // top color
      UIColor *colorTwo = [colorsArr objectAtIndex:0]; // bottom color
  
  
      NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
      CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
      CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
  
      // Apply gradient
  
      CGContextClipToMask(context, rect, self.CGImage);
      CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0,self.size.height * scale), 0);
      UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return gradientImage;
  }
  </code></pre>