Reputation: 459
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 ?
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
Reputation: 1371
When using autolayout, this could be useful:
class CircleImageView: UIImageView {
override func layoutSubviews() {
super.layoutSubviews()
cornerRadius = self.frame.width/2
}
}
Upvotes: 0
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
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
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
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
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
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