如何将UIImageView变成圆形[iOS]

4

我有一个UIImageView。我想把它变成圆形,并在其周围添加5像素的红色边框。

怎样才能实现呢?我分享了代码和图片,请有人帮帮我吗?

enter image description here

我的代码如下:

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

可能是重复的问题:IOS:如何创建带有圆角的UIImage或UIImageView - n00bProgrammer
7个回答

7

只需要按照这个代码,就能轻松做到。

跟着这个代码走即可。

#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];

5

尝试在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;

myImageview.bounds.size.width / 2 是调用“width”属性的正确方式。 - Frostmourne

1

解决方案(Swift 3):

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
    }
}

使用方法:

imageView?.circled.image = UIImage()

1

请确保您的项目中拥有QuartzCore框架,然后在创建此图像视图的文件中导入#import <QuartzCore/QuartzCore.h>

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];

0

引入QuartzCore框架,然后在你的.h文件中

#import <QuartzCore/QuartzCore.h>

然后只需按照以下步骤进行操作
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];

0

需要在您的项目中添加QuartzCore框架,然后在创建图像视图的文件中#import<QuartzCore/QuartzCore. h>

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;
}

我创建了一个函数,可重复使用它用于不同的UIImageView对象。

0

当使用自动布局时,这可能很有用:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接