IOS: create a UIImage or UIImageView with rounded corners IOS: create a UIImage or UIImageView with rounded corners ios ios

IOS: create a UIImage or UIImageView with rounded corners


Yes, it is possible.
Import the QuartzCore (#import <QuartzCore/QuartzCore.h>) header and play with the layer property of the UIImageView.

yourImageView.layer.cornerRadius = yourRadius;yourImageView.clipsToBounds = YES;

See the CALayer class reference for more info.


Try this Code For Round Image Import QuartzCore frameworksimple way to create Round Image

imageView.layer.backgroundColor=[[UIColor clearColor] CGColor];imageView.layer.cornerRadius=20;imageView.layer.borderWidth=2.0;imageView.layer.masksToBounds = YES;imageView.layer.borderColor=[[UIColor redColor] CGColor];

enter image description here


Objective-C

-(UIImage *)makeRoundedImage:(UIImage *) image                       radius: (float) radius;{  CALayer *imageLayer = [CALayer layer];  imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);  imageLayer.contents = (id) image.CGImage;  imageLayer.masksToBounds = YES;  imageLayer.cornerRadius = radius;  UIGraphicsBeginImageContext(image.size);  [imageLayer renderInContext:UIGraphicsGetCurrentContext()];  UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();  return roundedImage;}

Swift 3

func makeRoundedImage(image: UIImage, radius: Float) -> UIImage {    var imageLayer = CALayer()    imageLayer.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)    imageLayer.contents = image.cgImage    imageLayer.masksToBounds = true    imageLayer.cornerRadius = radius    UIGraphicsBeginImageContext(image.size)    imageLayer.render(in: UIGraphicsGetCurrentContext())    var roundedImage = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()    return roundedImage}