【问题标题】:How to make UIImageView as a circle [iOS]如何将 UIImageView 制作成圆形 [iOS]
【发布时间】:2014-07-02 08:12:51
【问题描述】:

我有一个UIImageView。我想把它做成圆形,周围有 5 像素的红色边框。

这怎么可能?我也分享代码和图像。请有人帮帮我吗?

我的代码是:

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

【问题讨论】:

标签: ios


【解决方案1】:

只要你能做到。

只需按照此代码..

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

【讨论】:

    【解决方案2】:

    在 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 是调用“宽度”的正确方法
    【解决方案3】:

    确保您的项目中有QuartzCore 框架,然后在您创建此图像视图的文件中导入#import &lt;QuartzCore/QuartzCore.h&gt;

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

    【讨论】:

      【解决方案4】:

      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()
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          需要在您的项目中添加 QuartzCore 框架,然后在您正在创建该图像视图的文件中添加#import&lt;QuartzCore/QuartzCore. h&gt;

          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 对象创建了一个函数。

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-09-15
              • 2011-11-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-07-20
              • 1970-01-01
              相关资源
              最近更新 更多