Making a UIImage to a circle form Making a UIImage to a circle form xcode xcode

Making a UIImage to a circle form


try this code

yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;yourImageView.layer.masksToBounds = YES;yourImageView.layer.borderWidth = 0;

this show image like ios 7 circle image thanks


This might help you, pass corner radius as half of the reference view where you want to display the image or you can set a custom rect,

eg. In my case I want to display the image on a "imageView", so the corner radius in this case would be imageView.frame.size.width/2

- (void)displayImage{    imageView.image = [self getRoundedRectImageFromImage:@"Test.png" onReferenceView:imageView withCornerRadius: imageView.frame.size.width/2];}- (UIImage *)getRoundedRectImageFromImage :(UIImage *)image onReferenceView :(UIImageView*)imageView withCornerRadius :(float)cornerRadius{    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds                                cornerRadius:cornerRadius] addClip];    [image drawInRect:imageView.bounds];    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return finalImage;}


Sorry for ugly formatting.Better version of aToz's answer.

@interface UIImage (CircleMask)+ (UIImage *)roundedRectImageFromImage :(UIImage *)image                                  size :(CGSize)imageSize                      withCornerRadius :(float)cornerRadius;@end@implementation UIImage(CircleMask)+(UIImage*)roundedRectImageFromImage:(UIImage *)image                                size:(CGSize)imageSize                    withCornerRadius:(float)cornerRadius{    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);   //  <= notice 0.0 as third scale parameter. It is important cause default draw scale ≠ 1.0. Try 1.0 - it will draw an ugly image..    CGRect bounds = (CGRect){CGPointZero,imageSize};    [[UIBezierPath bezierPathWithRoundedRect:bounds                                cornerRadius:cornerRadius] addClip];    [image drawInRect:bounds];    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return finalImage;}@end

The same thing in Swift:

extension UIImage{    class func roundedRectImageFromImage(image:UIImage,imageSize:CGSize,cornerRadius:CGFloat)->UIImage{        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)        let bounds = CGRect(origin: CGPointZero, size: imageSize)        UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()        image.drawInRect(bounds)        let finalImage = UIGraphicsGetImageFromCurrentImageContext()        UIGraphicsEndImageContext()        return finalImage    }    }