【问题标题】:IOS - Adding Image to UIImageView Causes Issues with Image ModeIOS - 将图像添加到 UIImageView 会导致图像模式出现问题
【发布时间】:2013-05-27 15:22:44
【问题描述】:

最近我观察到 UIImageView 有一些奇怪的行为,想知道是否有人可以提供一些 insite。我有两个 UIImageViews,其中的图像是从数据库加载的。在 Xcode 检查器中,我设置了剪辑子视图属性并将图像模式更改为方面填充。这很好用,当加载图像时,它们会在视图中正确显示并按应有的方式裁剪。但是,我想添加一个占位符图像,以防用户尚未上传。当我这样做时,图像没有调整大小并拉伸通过它的视图。我尝试以编程方式重置属性,如下所示,但没有任何改变。

//code used to try to reset the image mode and clipping of the sub-views
//set up user profile picture
_profilePic.contentMode = UIViewContentModeScaleAspectFit;
_profilePic.layer.masksToBounds = YES;
_profilePic.layer.borderColor = [UIColor whiteColor].CGColor;
_profilePic.layer.borderWidth = 3.0f;

//set up users cover photo
_coverPhoto.contentMode = UIViewContentModeScaleAspectFit;
_coverPhoto.clipsToBounds = YES;
_coverPhoto.layer.masksToBounds = YES;

为什么当我通过 Xcode 检查器将图像添加到视图时会发生这种情况,但当图像为空时不会发生这种情况?

【问题讨论】:

  • 我也遇到了同样的问题,有解决办法吗?

标签: ios xcode uiimageview


【解决方案1】:

您应该使用UIViewContentModeScaleAspectFill 而不是UIViewContentModeScaleAspectFit

【讨论】:

  • 不,这似乎没有帮助。几乎就像手动将图像放入 UIImageView 的行为会导致视图的属性被更改并且以编程方式更改它们没有任何效果。 @murat
【解决方案2】:

当我在 ViewDidLoad 的开头使用以下代码行添加它们时,我得到了相同的效果并且没有任何不需要的图像溢出。

_coverPhoto.image = [UIImage imageNamed:@"stockCoverPhoto.png"];
_profilePic.image = [UIImage imageNamed:@"stockProfilePhoto.png"];

不是信息最丰富的解决方案,因为它没有提供对此问题的洞察力,但它是一个解决方案。任何人都对如何解决此问题有任何其他意见,为什么会发生这种情况我很想听听,因为我还在摸不着头脑。

【讨论】:

    【解决方案3】:

    这里也一样.. setImage 到 UIImageView 后无法更改 contentMode..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多