简体   繁体   English

在 UIImageView 上设置角半径不起作用

[英]Setting Corner Radius on UIImageView not working

I'm at a bit of a loss.我有点不知所措。 I've used the layer property of UIView to round the corners of multiple elements in my app.我已经使用 UIView 的图层属性来圆化我应用程序中多个元素的角。 However, this one UIImageView is simply not complying.然而,这个 UIImageView 根本不符合要求。 Not sure what I am missing.不知道我错过了什么。

The UIImageView (called previewImage) is contained in a Table View Cell. UIImageView(称为 previewImage)包含在表视图单元格中。 I've tried setting the cornerRadius property multiple location (in the cell itself and in the controller that creates the cell) to no avail.我尝试将 cornerRadius 属性设置为多个位置(在单元格本身和创建单元格的 controller 中)无济于事。

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

Is there something about the way cells are loaded that I'm missing?我缺少有关细胞加载方式的某些信息吗?

You need to set the layer's masksToBounds property to YES :您需要将图层的masksToBounds属性设置为YES

cell.previewImage.layer.masksToBounds = YES;

This is because the UIImageView control creates a pseudo-subview to hold the UIImage object.这是因为UIImageView控件创建了一个伪子视图来保存UIImage对象。

Also worth noting that另外值得注意的是

  1. If you are using aspectFit AND cornerRadius with clipsToBounds/masksToBounds, you won't get the rounded corners.如果您使用带有 clipsToBounds/masksToBounds 的 aspectFit AND cornerRadius,您将不会得到圆角。

ie if you have this即如果你有这个

theImageView.contentMode = .scaleAspectFit

and

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

or或者

theImageView.layer.masksToBounds = true

It won't work .不会工作 you will have to get rid of aspectFit code你将不得不摆脱aspectFit代码

//theImageView.contentMode = .scaleAspectFit
  1. Make sure the width and the height for the Image View is same确保图像视图宽度和高度相同

This should work这应该工作

cell.previewImage.clipsToBounds = YES;

cell.previewImage.layer.cornerRadius = 20;

I believe you need to set:我相信你需要设置:

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

In Xcode Interface Builder, selecting 'Clip Subviews' Drawing attribute for the view together with setting the corner radius in the code cell.previewImage.layer.cornerRadius = 20;在 Xcode Interface Builder 中,为视图选择“Clip Subviews”绘图属性,并在代码cell.previewImage.layer.cornerRadius = 20;设置角半径cell.previewImage.layer.cornerRadius = 20; does the job for me!为我做这项工作!

See 'Clip Subviews' option in IB请参阅 IB 中的“剪辑子视图”选项

Try this below piece of code试试下面这段代码

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

Try this code:-试试这个代码:-

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

For imageView.contentMode = .scaleAspectFill the cornerRadius is not applied if the image is very large, depending on the hardware.对于imageView.contentMode = .scaleAspectFill ,如果图像非常大,则不应用cornerRadius ,具体取决于硬件。

Some tests with resized panorama images:一些调整大小的全景图像的测试:

  • iPhone X, image size 5000x1107: 🚫 4000x886: ✅ iPhone X,图像尺寸 5000x1107:🚫 4000x886:✅
  • iPhone 6s Plus, image size 10000x2215: 🚫 5000x1107: ✅ iPhone 6s Plus,图像尺寸 10000x2215:🚫 5000x1107:✅
  • iPhone 6s, image size 10000x2215: 🚫 5000x1107: ✅ iPhone 6s,图像大小 10000x2215:🚫 5000x1107:✅

The imageView dimensions where 160x100. imageView 尺寸为 160x100。 Interestingly, the newer hardware isn't necessarily more capable.有趣的是,较新的硬件不一定更强大。

Feel free to edit this post with more test results!随意编辑这篇文章以获取更多测试结果!

UIImageView gives cornerRadius with clipsToBounds folowing is example of Swift and Objectiv-C (The followfing example code for round image) UIImageView 给出 cornerRadius 和 clipsToBounds 以下是 Swift 和 Objectiv-C 的示例(圆形图像的以下示例代码)

Swift Swift

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

Objectiv -C目标-C

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

Swift 4.2 Answer:斯威夫特 4.2 答案:

In order for the corner radius to work, add the image to a UIView and then you need to set the image's masksToBounds property to true :为了使圆角半径起作用,请将图像添加到UIView ,然后您需要将图像的masksToBounds属性设置为true

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

Note: Replace 20 by the desired cornerRadius注意:将 20 替换为所需的cornerRadius

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

Nothing from the previous answers is working?以前的答案没有任何效果?

It may happen that the size of the UIImageView is bigger than the image size.可能会发生 UIImageView 的大小大于图像大小的情况。 Corner radius can be set well but not visible in that case.拐角半径可以设置得很好,但在这种情况下不可见。

Quick check of the UIImageView size by code: (or can use "View UI Hierarchy" tool in XCode)通过代码快速检查 UIImageView 大小:(或可以使用 XCode 中的“查看 UI 层次结构”工具)

self.imageView.backgroundColor = [UIColor greenColor]; 

In this scenario you should assure that UIImageView has the same aspect ratio as the image.在这种情况下,您应该确保 UIImageView 具有与图像相同的纵横比。

You can use this code您可以使用此代码

override func viewDidLayoutSubviews() { 
    yourImageView.layer.cornerRadius = yourImageView.frame.width / 2
}

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

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