【问题标题】:UIImage should not stretched inside the UIImageViewUIImage 不应在 UIImageView 内拉伸
【发布时间】:2015-05-13 06:19:52
【问题描述】:

我在我的 xib 中使用UIImageView。应用程序专为没有自动布局的多设备而设计。这意味着我正在使用自动调整大小。我想做的是,只有我的UIImageView 应该自动调整UIImageView 内的图像而不是我的图像,但不幸的是我的图像也被UIImageView 拉伸。我尝试了不同的方法,但都没有成功。也更改了内容模式,但对我不起作用。

【问题讨论】:

  • 不可能。 UIImage 会根据 UIImageView 的大小进行拉伸。
  • @iPhone 有什么棘手的解决方案,哪位冷帮我?
  • 但是你为什么想要这样的解决方案?
  • 我认为你应该保持 UIImageView 的大小固定。在我看来没有其他解决方案
  • 请在屏幕截图中显示您的问题@Salman

标签: ios objective-c iphone uiimageview uiimage


【解决方案1】:

试试这个……

//set content mode
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//clear background color
self.imageView.backgroundColor=[UIColor clearColor];

UIViewContentMode

UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,

【讨论】:

  • 这个解决方案是在 UIImageView 而不是 UIImage 上实现的。
  • @Salman 我已经尝试过了,但是可以。你可以在这里看到链接stackoverflow.com/questions/7035523/…
  • @Salman 请分享您的输出和预期输出的屏幕截图。
  • @AshokLondhe 我已经发布了截图。我的图像尺寸是 480 X 360 ,我的 UIImageView 根据 iPad 调整大小,但我不想让我的图像调整大小或拉伸。
  • @Salman 你看过我在之前评论中提到的链接吗?
【解决方案2】:

不要将UIImage 添加到大小将发生变化的UIImageView。将其添加到另一个 UIImageView 并让第二个 UIImageView 成为第一个 UIImageViewsubview

UIImage *exampleImage = [UIImage imageWithContentsOfFile:filePath];
UIImageView *variableImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

CGFloat imageX = (variableImageView.bounds.size.width - exampleImage.size.width) / 2;
CGFLoat imageY = (variableImageView.bounds.size.height - exampleImage.size.height) / 2;

UIImageView *helperImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, exampleImage.size.width, exampleImage.size.height)];

helperImageView.image = exampleImage;
helperImageView.contentMode = UIViewContentModeScaleAspectFit;

[self.view addSubview:variableImageView];
[variableImageView addSubview:helperImageView];

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2014-01-17
    • 2015-04-26
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多