Skip to content

Instantly share code, notes, and snippets.

@radiovisual
Last active August 29, 2015 14:16
Show Gist options
  • Save radiovisual/e47b6383272f5bd1b701 to your computer and use it in GitHub Desktop.
Save radiovisual/e47b6383272f5bd1b701 to your computer and use it in GitHub Desktop.
Convert your rectangular image into a perfect circle image with transparent bg.
// @param {UIImage} The rectangle image you want to convert to a round image
// @param {CGSize} The size (width and height) of the resulting round image
// @return {NSData} the PNGRepresentation of the resulting round image
-(NSData *)rectangleImageToTransparentRoundImageFromImage:(UIImage *)image withSize:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImageView *_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
_imgView.image = image;
_imgView.contentMode = UIViewContentModeScaleAspectFit;
_imgView.layer.cornerRadius = size.width / 2;
_imgView.clipsToBounds = YES;
[_imgView.layer renderInContext:context];
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