Change RGB color of the picture

  1. - (UIImage *) changeColor: (UIImage *)image {
  2.     UIGraphicsBeginImageContext(image.size);
  3.    
  4.     CGRect contextRect;
  5.     contextRect.origin.x = 0.0f;
  6.     contextRect.origin.y = 0.0f;
  7.     contextRect.size = [image size];
  8.     // Retrieve source image and begin image context
  9.     CGSize itemImageSize = [image size];
  10.     CGPoint itemImagePosition;
  11.     itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
  12.     itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) );
  13.    
  14.     UIGraphicsBeginImageContext(contextRect.size);
  15.    
  16.     CGContextRef c = UIGraphicsGetCurrentContext();
  17.     // Setup shadow
  18.     // Setup transparency layer and clip to mask
  19.     CGContextBeginTransparencyLayer(c, NULL);
  20.     CGContextScaleCTM(c, 1.0, -1.0);
  21.     CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [image CGImage]);
  22.     // Fill and end the transparency layer
  23.    
  24.     switch (colorSelected) {
  25.         case 0:
  26.             CGContextSetRGBFillColor(c, 0, 0, 1, 1);
  27.             break;
  28.  
  29.         default:
  30.             CGContextSetRGBFillColor(c, 1, 0, 0., 1);
  31.             break;
  32.     }
  33.    
  34.     contextRect.size.height = -contextRect.size.height;
  35.     contextRect.size.height -= 15;
  36.     CGContextFillRect(c, contextRect);
  37.     CGContextEndTransparencyLayer(c);
  38.  
  39.     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  40.     UIGraphicsEndImageContext();
  41.     return img;
  42. }