【问题标题】:Setting Corner Radius on UIImageView not working在 UIImageView 上设置角半径不起作用
【发布时间】:2011-05-17 21:51:12
【问题描述】:

我有点不知所措。我已经使用 UIView 的 layer 属性来圆我应用程序中多个元素的角。但是,这一个 UIImageView 根本不符合要求。不知道我错过了什么。

UIImageView(称为 previewImage)包含在 Table View Cell 中。我尝试将cornerRadius 属性设置为多个位置(在单元格本身和创建单元格的控制器中)无济于事。

static NSString *CellIdentifier = @"MyTableViewCell";

MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
    cell.previewImage.layer.cornerRadius = 20; //Made it 20 to make sure it's obvious.
}

我缺少关于加载单元格的方式吗?

【问题讨论】:

    标签: iphone ios uiimageview rounded-corners


    【解决方案1】:

    需要将图层的masksToBounds属性设置为YES

    cell.previewImage.layer.masksToBounds = YES;

    这是因为UIImageView 控件创建了一个伪子视图来保存UIImage 对象。

    【讨论】:

    • 除非您还栅格化视图 (view.layer.shouldRasterize = YES),否则每一帧都需要重新遮盖所有像素。
    • @jjxtra 所以最好设置shouldRasterize = false?
    • 如果 shouldRasterize 为 false,则每帧都要支付掩码创建开销。如果 shouldRasterize 为 true 并且您的图层每帧都更改,则您需要支付掩码开销和光栅化开销。理想情况是一个静态(不经常更改)图层,且 shouldRasterize = true。
    【解决方案2】:

    相信你需要设置:

    cell.previewImage.layer.masksToBounds = YES;
    cell.previewImage.layer.opaque = NO;
    

    【讨论】:

      【解决方案3】:

      这应该可以工作

      cell.previewImage.clipsToBounds = YES;
      
      cell.previewImage.layer.cornerRadius = 20;
      

      【讨论】:

      • 不确定clipsToBounds 是不是后期类的方法。相信上面是masksToBounds
      • @AlfieHanssen 层有 maskToBounds:,视图有 clipsToBounds:
      【解决方案4】:

      在 Xcode Interface Builder 中,为视图选择 'Clip Subviews' 绘图属性并在代码 cell.previewImage.layer.cornerRadius = 20;中设置圆角半径为我完成了这项工作!

      See 'Clip Subviews' option in IB

      【讨论】:

        【解决方案5】:

        试试下面这段代码

        cell.previewImage.layer.cornerRadius = 20;
        cell.previewImage.clipsToBounds = YES;
        

        【讨论】:

          【解决方案6】:

          另外值得注意的是

          1. 如果您将 aspectFit ANDcornerRadius 与 clipsToBounds/masksToBounds 一起使用,您将不会得到圆角。

          即如果你有这个

          theImageView.contentMode = .scaleAspectFit
          

             theImageView.layer.cornerRadius = (theImageView.frame.size.height)/2
              theImageView.clipsToBounds = true
          

          theImageView.layer.masksToBounds = true
          

          行不通。你将不得不摆脱 aspectFit 代码

          //theImageView.contentMode = .scaleAspectFit
          
          1. 确保图像视图的宽度和高度相同

          【讨论】:

          • 简而言之:如果要使用 AspectFit,请设置纵横比 1:1
          • 这行不通。你将不得不摆脱 aspectFit 代码这对我有帮助
          【解决方案7】:
          -(void) viewDidAppear:(BOOL)animated{
                 [super viewDidAppear:animated];
                 [self setMaskTo:viewDistance 
                       byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
                     }
          
          - (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
               {
                UIBezierPath *rounded = [UIBezierPath 
                          bezierPathWithRoundedRect:view.bounds
                                                        byRoundingCorners:corners
          
                               cornerRadii:CGSizeMake(20.0, 20.0)];
          
                    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
                    shape.frame = self.view.bounds;
                   [shape setPath:rounded.CGPath];
                    view.layer.mask = shape;
                 }
          

          【讨论】:

            【解决方案8】:

            试试这个代码:-

            self.imgaviewName.clipsToBounds = true
            self.imageviewName.layer.cornerRadius = 10
            

            【讨论】:

            • 不鼓励使用纯代码的答案。请单击edit 并添加一些词来总结您的代码如何解决问题,或者解释您的答案与之前的答案/答案有何不同。谢谢
            【解决方案9】:

            Swift 4.2 答案:

            为了使圆角半径起作用,请将图像添加到UIView,然后您需要将图像的masksToBounds 属性设置为true

            planeImage.layer.masksToBounds = true
            planeImage.layer.cornerRadius = 20
            

            注意:将 20 替换为所需的 cornerRadius

            【讨论】:

              【解决方案10】:

              以前的答案没有任何效果?

              可能会发生 UIImageView 的大小大于图像大小的情况。拐角半径可以很好地设置,但在这种情况下不可见。

              通过代码快速检查 UIImageView 的大小:(或者可以使用 XCode 中的“View UI Hierarchy”工具)

              self.imageView.backgroundColor = [UIColor greenColor]; 
              

              在这种情况下,您应该确保 UIImageView 与图像具有相同的纵横比。

              【讨论】:

              • 这是我的问题 - 设置颜色显示它正在工作,但我的图像比我的图像视图小。在我的情况下,我最终使背景颜色与图像的背景相匹配,因此它看起来是圆形的
              【解决方案11】:

              对于imageView.contentMode = .scaleAspectFill,如果图像非常大,则不会应用cornerRadius,具体取决于硬件。

              一些调整大小的全景图像的测试:

              • iPhone X,图像尺寸 5000x1107:? 4000x886:✅
              • iPhone 6s Plus,图像尺寸 10000x2215:? 5000x1107:✅
              • iPhone 6s,图像尺寸 10000x2215:? 5000x1107:✅

              imageView 尺寸为 160x100。 有趣的是,较新的硬件不一定更强大。

              请随意编辑这篇文章以获取更多测试结果!

              【讨论】:

                【解决方案12】:

                UIImageView 给cornerRadius 和clipsToBounds 以下是Swift 和Objectiv-C 的示例(以下是圆形图像的示例代码)

                斯威夫特

                myImageView.layer.cornerRadius = myImageView.frame.size.height/2.0
                myImageView.clipsToBounds = true
                

                Objectiv -C

                [myImageView.layer setCornerRadius:myImageView.frame.size.height/2.0];
                [myImageView setClipsToBounds:TRUE];
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-12-03
                  • 1970-01-01
                  • 2013-05-09
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多