简体   繁体   English

圆角图像

[英]Rounded Corner images

I've been trying to do perfectly shaped circular images with non-square images. 我一直在尝试用非正方形图像制作形状完美的圆形图像。 I tried to make it with 我试图做到

[self.photoView.layer setCornerRadius:50.0f];
[self.photoView.layer setMasksToBounds:YES];

It becomes something like this: 它变成了这样的东西:

没有那么完美的舍入

I want to make it full perfect circular. 我要使其成为完整的完美圆形。

PhotoView's content mode is setted to Aspect Fill, It's height and width fixed to 80x80 with autolayout. PhotoView的内容模式设置为“纵横比填充”,通过自动布局将其高度和宽度固定为80x80。 I could not figure it out. 我不知道。

I made circular images with cropping and scaling them, but also want to add a border to it, in this way i need to recreate new uiimage to draw borders in it. 我通过裁剪和缩放来制作圆形图像,但还想为其添加边框,这样,我需要重新创建新的uiimage来在其中绘制边框。 It's expensive thing. 这是昂贵的东西。 I want to use photoView's layer to do this. 我想使用photoView的图层来执行此操作。

Any help is appreciated. 任何帮助表示赞赏。

You have to set corner radius is half of width/height (for square object only) so it will create round view. 您必须将角半径设置为width/height一半(仅适用于正方形对象),以便创建圆形视图。

[self.photoView.layer setCornerRadius:self.photoView.frame.size.height/2];

or set it to 40.0f because your height and width is 40x40. 或将其设置为40.0f因为您的高度和宽度为40x40。

Try this code, corner radius should be half of the width or height of the provided photo view and it must be square in shape. 尝试使用此代码,拐角半径应为所提供照片视图的widthheight的一半,并且形状必须为正方形。

So the code go like this: 因此,代码如下所示:

[self.photoView.layer setCornerRadius:CGRectGetHeight(self.photoView.frame)/2];
[self.photoView.layer setMasksToBounds:YES];

You can achieve this by using layer and mask: 您可以通过使用图层和蒙版来实现:

CGFloat imWidth = photoView.frame.size.width;
CGFloat imHeight = photoView.frame.size.height;
CGFloat minSize = imWidth < imHeight ? imWidth : imHeight;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = CGRectMake(imWidth * 0.5f - minSize * 0.5f, imHeight * 0.5f - minSize * 0.5f, minSize, minSize);
maskLayer.path = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, minSize, minSize), nil);
photoView.layer.mask = maskLayer;

This crop the photoview according to a circle inside the photoview.frame . 这将根据photoview.frame内部的圆圈photoview.frame You can change minSize to: CGFloat maxSize = imWidth > imHeight ? imWidth : imHeight; 您可以将minSize更改为: CGFloat maxSize = imWidth > imHeight ? imWidth : imHeight; CGFloat maxSize = imWidth > imHeight ? imWidth : imHeight; if you want another effect. 如果您想要其他效果。

try this Import the QuartzCore #import <QuartzCore/QuartzCore.h> header and play with the layer property of the UIImageView . 试试这个导入QuartzCore #import <QuartzCore/QuartzCore.h>标头,并使用UIImageView的layer属性。

self.photoView.layer.cornerRadius = self.photoView.frame.size.height/2;
self.photoView.clipsToBounds = YES;

try this code, 试试这个代码,

swift code: SWIFT代码:

declare 宣布

@IBOutlet weak var circleView: UIView!

viewDidLoad or any method: viewDidLoad或任何方法:

circleView.layer.cornerRadius = circleView.frame.width/2
circleView.layer.borderWidth = 10
circleView.layer.borderColor = UIColor.blackColor().CGColor

objective-C code: 目标C代码:

declare UIImageView 声明UIImageView

@property (weak, nonatomic) IBOutlet UIImageView *myimageview;

viewDidLoad or any method, viewDidLoad或任何方法,

myimageview.layer.cornerRadius = myimageview.frame.size.width/2;
myimageview.layer.masksToBounds = YES;
myimageview.layer.borderWidth = 2.0;
myimageview.layer.borderColor = [UIColor blackColor].CGColor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM