简体   繁体   English

如何使 UIImageView 成为一个圆圈 [iOS]

[英]How to make UIImageView as a circle [iOS]

I have an UIImageView .我有一个UIImageView I want to make it as a circle type and 5 pixel red color border around it.我想把它做成圆形,周围有 5 个像素的红色边框。

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

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

Try this on the UIImageView:在 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;

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.确保您的项目中有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];

Swift 3 Solution:斯威夫特 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
    }
}

usage:用法:

imageView?.circled.image = UIImage()

import QuartzCore framework then in your .h file导入QuartzCore框架,然后在您的 .h 文件中

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

Need to add QuartzCore framework in your project, then #import<QuartzCore/QuartzCore. h>需要在你的项目中添加QuartzCore框架,然后#import<QuartzCore/QuartzCore. h> #import<QuartzCore/QuartzCore. h> in the file where you're creating that image view. #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;
}

I make a function for reuse it for different UIImageView objects.我制作了一个函数,用于将它重用于不同的UIImageView对象。

When using autolayout, this could be useful:使用自动布局时,这可能很有用:

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

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

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