I have been trying to mask a UIImage into a circle. I\'m using now the code that has been popular on other answers here, but although I do get a circle its edges are very ja
If you're using Swift, you can import 'AlamofireImage', and use one of its funcs. For circled image:
let roundImage = UIImage(named: "myImage")!.af_imageRoundedIntoCircle()
Felt like I should throw my hat into the ring. An effective, compact Swift 5 solution:
extension UIImage {
var rounded: UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
defer { UIGraphicsEndImageContext() }
let bounds = CGRect(origin: .zero, size: size)
UIBezierPath(roundedRect: bounds, cornerRadius: size.height/2.0).addClip()
draw(in: bounds)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
A better version of aToz's and fnc12's answers (in Swift):
extension UIImage
{
func roundImage() -> UIImage
{
let newImage = self.copy() as! UIImage
let cornerRadius = self.size.height/2
UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
let bounds = CGRect(origin: CGPointZero, size: self.size)
UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
newImage.drawInRect(bounds)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}
}
Usage:
self.userImageView.image = image.roundedImage()
If you are using an UIImageView
and you are looking to animate the size of the image while maintaining the rounded corners, you can apply a transformation as follows:
Objective-C
// Our UIImageView reference
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- (void)viewDidLoad {
[super viewDidLoad];
myImageView.layer.cornerRadius = myImageView.frame.size.width / 2.0f;
myImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4f, 0.4f);
}
- (void)viewDidAppear:(BOOL)animated {
[UIView animateWithDuration:1.0 animations:^{
myImageView.transform = CGAffineTransformIdentity;
}];
}
Swift
// Our UIImageView reference
@IBOutlet weak var myImageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myImageView.layer.cornerRadius = myImageView.frame.size.width / 2;
myImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4)
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(1.0) {
myImageView.transform = CGAffineTransformIdentity;
}
}
NOTE: This will also maintain any AutoLayout constraints
.
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
What about getting help from UIImageView
+ (UIImage *)circularImage:(UIImage *)image withDiameter:(NSUInteger)diameter
{
CGRect frame = CGRectMake(0.0f, 0.0f, diameter, diameter);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.image = image;
CALayer *layer = imageView.layer;
layer.masksToBounds = YES;
layer.cornerRadius =MAX( imageView.frame.size.height,imageView.frame.size.width)/2;
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size,NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[layer renderInContext:context];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}