【问题标题】:Image disappears when SetMasksToBounds:YESSetMasksToBounds:YES 时图像消失
【发布时间】:2015-04-03 01:18:18
【问题描述】:

引用this answer,我正在尝试使 UIImageView 显示圆形图像。我生成了一个蒙版并尝试使用该图像对其进行蒙版,但也没有用。

当我尝试遮盖图像或更改角落时,没有任何反应。 就是这样: [proPic.layer setCornerRadius:proPic.frame.size.width/2];

当我打电话给[proPic.layer setMasksToBounds:YES]; 时,图像完全消失了。当我尝试使用third party library 来屏蔽图像时也是如此。

有什么建议吗?

编辑:

//Profile Picture
proPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DummyProPic"]];
//Mask Profile Picture to be circular
[proPic.layer setCornerRadius:proPic.frame.size.width/2];
//With this line added, the picture disappears
//Without this line, nothing is done to mask the image
[proPic.layer setMasksToBounds:YES];

[self.contentView addSubview:proPic];
[proPic mas_makeConstraints:^(MASConstraintMaker *make) {

    make.top.equalTo(self.contentView.mas_top).with.offset(padding.top);
    make.centerX.equalTo(self.contentView.mas_centerX);
    make.height.equalTo(@120);
    make.width.equalTo(@120);
}];

【问题讨论】:

  • 你能发布你的代码吗?
  • 您的代码看起来不错,您确定proPic 是用IBOutlet 声明的,并且在左侧(行号旁边),有一个小实心圆圈表示有与您的故事板的连接(假设您正在使用它)?
  • 我没有使用故事板。我会看看我是否有更多相关的代码。
  • 如果去掉cornerRadius代码,图像是否仍然丢失?
  • 不,如果我移除角线,图像将保持未遮盖状态,就好像什么都没做一样。

标签: ios user-interface calayer


【解决方案1】:

您应该删除 .layer 以屏蔽边界。试试:

[proPic setMasksToBounds:YES];

【讨论】:

  • 不是 UIImageView 的方法
【解决方案2】:
[proPic.layer setCornerRadius:60];
[proPic.layer setMasksToBounds:YES];

这行得通,它不喜欢proPic.frame.size.width/2 部分。

【讨论】:

    【解决方案3】:

    如果setMasksToBounds 造成问题,则删除屏蔽,您可以尝试以下操作

    proPic.clipsToBounds=YES;

    它可以帮助你。

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2015-12-12
      • 2023-03-15
      相关资源
      最近更新 更多