user3631436
user3631436

Reputation: 459

How to make UIImageView as a circle [iOS]

I have an UIImageView. I want to make it as a circle type and 5 pixel red color border around it.

How it is possible? I share code and image also. Please anybody help me ?

enter image description here

My code is:

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)];
[myImageview setImage:[UIImage imageNamed:@"image.png"]];
[self.view addSubview:myImageview];

Upvotes: 4

Views: 4604

Answers (7)

Kamen Dobrev
Kamen Dobrev

Reputation: 1371

When using autolayout, this could be useful:

class CircleImageView: UIImageView {
    override func layoutSubviews() {
        super.layoutSubviews()
        cornerRadius = self.frame.width/2
    }
}

Upvotes: 0

Maor
Maor

Reputation: 3430

Swift 3 Solution:

extension UIImageView{
    var circled : UIImageView{
        self.layer.cornerRadius = self.frame.width / 2;
        self.layer.borderWidth = 2
        self.layer.borderColor = UIColor.red.cgColor
        self.clipsToBounds = true
        return self
    }
}

usage:

imageView?.circled.image = UIImage()

Upvotes: 1

Hemang
Hemang

Reputation: 27072

Need to add QuartzCore framework in your project, then#import<QuartzCore/QuartzCore. h> in the file where you're creating that image view.

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)];
[myImageview setImage:[UIImage imageNamed:@"image.png"]];
myImageview.clipsToBounds = YES:
[self.view addSubview:myImageview];
[self updateImageViewLayer:myImageview.layer withBorderColor:[UIColor redColor] borderWidth:5.f cornerRadius:(myImageview.bounds.width/2.f)];

- (void)updateImageViewLayer:(CALayer*)layer withBorderColor:(UIColor *)color borderWidth:(CGFloat)width cornerRadius:(CGFloat)radius
{
    layer.borderWidth = width;
    layer.borderColor = color.CGColor;
    layer.cornerRadius = radius / 2.f;
    layer.masksToBounds = YES;
}

I make a function for reuse it for different UIImageView objects.

Upvotes: 0

Pancho
Pancho

Reputation: 4143

import QuartzCore framework then in your .h file

#import <QuartzCore/QuartzCore.h>

Then just do the following

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)];
[myImageview setImage:[UIImage imageNamed:@"image.png"]];
myImageview.layer.cornerRadius = 30;
myImageview.layer.borderWidth = 5.0; // or whatever width you want to apply
myImageview.layer.borderColor = [[UIColor redColor] CGColor];
[self.view addSubview:myImageview];

Upvotes: 0

rckoenes
rckoenes

Reputation: 69499

make sure you have the QuartzCore framework in you project, then import #import <QuartzCore/QuartzCore.h> in the file where you create this image view.

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)];
[myImageview setImage:[UIImage imageNamed:@"image.png"]];

myImageview.clipsToBounds = YES:
myImageview.layer.masksToBounds = YES;
myImageview.layer.cornerRadius = myImageview.bounds.width / 2.0f;
myImageview.layer.borderWidth = 5.0;
myImageview.layer.borderColor = [UIColor redColor];


[self.view addSubview:myImageview];

Upvotes: 1

Soumya Ranjan
Soumya Ranjan

Reputation: 4843

Simply you can make it.

Just follow this code..

#import <QuartzCore/QuartzCore.h>

[myImageview.layer setBorderColor:[[UIColor redColor] CGColor]]; // For border color
[myImageview.layer setBorderWidth:5.0]; // For Border width
[myImageview.layer setCornerRadius:45.0f]; // For Corner radious
[myImageview.layer setMasksToBounds:YES];

Upvotes: 7

corn&#233;
corn&#233;

Reputation: 798

Try this on the UIImageView:

myImageview.layer.cornerRadius = myImageview.width / 2;
myImageview.layer.masksToBounds = YES;
myImageview.layer.borderWidth = 5.0f;
myImageview.layer.borderColor = [[UIColor redColor] CGColor];

myImageview.contentMode = UIViewContentModeCenter;

Upvotes: 5

Related Questions