I have an image which is a black ring:
black ring http://imageshack.us/a/img51/2029/blackcb.png
In my view, I need to display it as a white ring. So, to tint the image, I have written the below method:
- (UIImage *)getRingImage
{
UIImage *ringImage = [UIImage imageNamed:@"ring"];
CGFloat scale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContext(CGSizeMake(ringImage.size.width * scale, ringImage.size.height * scale));
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, ringImage.size.width * scale, ringImage.size.height * scale);
// Converting a UIImage to a CGImage flips the image,
// so apply a upside-down translation
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -rect.size.height);
// Set the fill color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextSetFillColorSpace(ctx, colorSpace);
// Set the mask to only tint non-transparent pixels
CGContextClipToMask(ctx, rect, ringImage.CGImage);
// Set the fill color
CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1].CGColor);
UIRectFillUsingBlendMode(rect, kCGBlendModeColor);
ringImage = UIGraphicsGetImageFromCurrentImageContext();
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
return ringImage;
}
This code works fine. I want to know if there is a better way to do this.