Last active
August 29, 2015 14:16
-
-
Save radiovisual/e47b6383272f5bd1b701 to your computer and use it in GitHub Desktop.
Convert your rectangular image into a perfect circle image with transparent bg.
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
// @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