Skip to content

Instantly share code, notes, and snippets.

@trungtran
Created February 21, 2013 16:50
Show Gist options
  • Save trungtran/5006143 to your computer and use it in GitHub Desktop.
Save trungtran/5006143 to your computer and use it in GitHub Desktop.
+ (UIImage *)iconImageFrom:(VKColoredAssetConfigObject *)iconObject
{
UIImage *maskImage = iconObject.maskImage;
UIImage *overlayImage = iconObject.overlayImage;
CGSize size = maskImage.size;
CGFloat scale = [[UIScreen mainScreen] scale];
CGRect rect = CGRectMake(0.0, 0.0, size.width * scale, size.height * scale);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create Context
CGContextRef context = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
if (!context)
{
CGColorSpaceRelease(colorSpace);
return nil;
}
// Draw
{
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextSaveGState(context);
// Mask
CGContextClipToMask(context, rect, maskImage.CGImage);
// Gradient
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
// Overlay
CGContextDrawImage(context, rect, overlayImage.CGImage);
}
// Create resulting image
CGImageRef resultImageRef = CGBitmapContextCreateImage(context);
UIImage *resultImage = [[UIImage alloc] initWithCGImage:resultImageRef scale:scale orientation:UIImageOrientationUp];
// Clean up
CGImageRelease(resultImageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return resultImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment