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

发布于 2024-10-05 08:39:49 字数 668 浏览 9 评论 0原文

我有点不知所措。我使用 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.
}

我缺少有关单元格加载方式的信息吗?

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. However, this one UIImageView is simply not complying. Not sure what I am missing.

The UIImageView (called previewImage) is contained in a Table View Cell. I've tried setting the cornerRadius property multiple location (in the cell itself and in the controller that creates the cell) to no avail.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

野稚 2024-10-12 08:39:50

另外值得注意的是,

  1. 如果您将aspectFitANDcornerRadius与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. 确保图像视图的宽度和高度相同

Also worth noting that

  1. If you are using aspectFit AND cornerRadius with clipsToBounds/masksToBounds, you won't get the rounded corners.

i.e 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

//theImageView.contentMode = .scaleAspectFit
  1. Make sure the width and the height for the Image View is same
第几種人 2024-10-12 08:39:50

这应该有效

cell.previewImage.clipsToBounds = YES;

cell.previewImage.layer.cornerRadius = 20;

This should work

cell.previewImage.clipsToBounds = YES;

cell.previewImage.layer.cornerRadius = 20;
贱贱哒 2024-10-12 08:39:50

我相信你需要设置:

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

I believe you need to set:

cell.previewImage.layer.masksToBounds = YES;
cell.previewImage.layer.opaque = NO;
时光磨忆 2024-10-12 08:39:50

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

查看 IB 中的“剪辑子视图”选项

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;does the job for me!

See 'Clip Subviews' option in IB

牛↙奶布丁 2024-10-12 08:39:50

UIImageView 为cornerRadius 提供 ClipsToBounds 以下是 Swift 和 Objectiv-C 的示例(以下是圆形图像的示例代码)

Swift

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

UIImageView gives cornerRadius with clipsToBounds folowing is example of Swift and Objectiv-C (The followfing example code for round image)

Swift

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];
心作怪 2024-10-12 08:39:50

试试下面的代码

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

Try this below piece of code

cell.previewImage.layer.cornerRadius = 20;
cell.previewImage.clipsToBounds = YES;
毁梦 2024-10-12 08:39:50

试试这个代码:-

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

Try this code:-

self.imgaviewName.clipsToBounds = true
self.imageviewName.layer.cornerRadius = 10
阳光的暖冬 2024-10-12 08:39:50

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

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

  • iPhone X,图像尺寸 5000x1107:

For imageView.contentMode = .scaleAspectFill the cornerRadius is not applied if the image is very large, depending on the hardware.

Some tests with resized panorama images:

  • iPhone X, image size 5000x1107: ???? 4000x886: ✅
  • iPhone 6s Plus, image size 10000x2215: ???? 5000x1107: ✅
  • iPhone 6s, image size 10000x2215: ???? 5000x1107: ✅

The imageView dimensions where 160x100.
Interestingly, the newer hardware isn't necessarily more capable.

Feel free to edit this post with more test results!

此刻的回忆 2024-10-12 08:39:50

Swift 4.2 答案:

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

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

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

Swift 4.2 Answer:

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:

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

Note: Replace 20 by the desired cornerRadius

瞄了个咪的 2024-10-12 08:39:50

以前的答案没有任何作用?

UIImageView 的尺寸可能大于图像尺寸。圆角半径可以设置好,但在这种情况下不可见。

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

self.imageView.backgroundColor = [UIColor greenColor]; 

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

Nothing from the previous answers is working?

It may happen that the size of the UIImageView is bigger than the image size. 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)

self.imageView.backgroundColor = [UIColor greenColor]; 

In this scenario you should assure that UIImageView has the same aspect ratio as the image.

猥琐帝 2024-10-12 08:39:50
-(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;
       }
-(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;
       }
辞别 2024-10-12 08:39:50

您可以使用此代码

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

You can use this code

override func viewDidLayoutSubviews() { 
    yourImageView.layer.cornerRadius = yourImageView.frame.width / 2
}
烟若柳尘 2024-10-12 08:39:49

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

cell.previewImage.layer.masksToBounds = YES;

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

You need to set the layer's masksToBounds property to YES:

cell.previewImage.layer.masksToBounds = YES;

This is because the UIImageView control creates a pseudo-subview to hold the UIImage object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文