Created
March 11, 2015 11:42
-
-
Save radiovisual/c202eff65fc83a850d73 to your computer and use it in GitHub Desktop.
Make a perfect-circle cutout of any image in Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This function will create perfect-circle cutout of any image you supply. | |
// So if you have a solid black image, the resulting PNG will be a "cutout-out" (transparent) | |
// circle with a border of solid black. It will return NSData (for use with Parse's PFFile), | |
// but can be easily modified to return the resulting UIImage (*img) | |
// | |
// @param {UIImage} The image you want to "cutout" | |
// @param {CGSize} The size (width and height) you want the resulting image to be | |
// return {NSData} the PNGrepresentation of the resulting UIImage | |
-(NSData *)createCircleCutOutofImage:(UIImage *)image withSize:(CGSize)size { | |
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
[image drawInRect:CGRectMake(0, 0, size.width, size.height)]; | |
CGRect clippingEllipseRect = CGRectMake(0, 0, size.width, size.width); | |
CGContextAddEllipseInRect(context, clippingEllipseRect); | |
CGContextClip(context); | |
CGContextClearRect(context, clippingEllipseRect); | |
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
// return NSData | |
return UIImagePNGRepresentation(img); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment