【问题标题】:Resizing image to fit UIImageView调整图像大小以适应 UIImageView
【发布时间】:2012-09-15 04:33:31
【问题描述】:

我对目标 c 非常陌生,我刚刚开始了解自己的方位。我想做一些非常简单的事情,但事实证明这是一个相当大的挑战:

我正在尝试将图像显示到UIImageView。我显示的图像很大,我希望它按比例缩小以适合UIImageView。我尝试设置AspectFit 查看模式,但图像显示为原始大小并被UIImageView 剪切。我的代码如下:

- (void)changeImages
{
    UIImage* img11 = nil;

    img11 = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"jpeg"]];

    u11.contentMode = UIViewContentModeScaleAspectFit;
    u11.image = img11;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self changeImages];
}

谁能解释一下这个问题?

谢谢!

【问题讨论】:

  • 你读过stackoverflow.com/questions/185652/… 吗?尤其是第二个答案可能很有趣。
  • 所有权利,这应该有效。检查事项:您是否已经在笔尖/情节提要中设置了图像?如果是这样,这可能会掩盖未调用的代码。是否调用了 changeImages?您可以拨打NSLog(@"changeImages called") 来查看是否是,或者使用断点。如果不是,您可能忘记将 nib/storyboard 中的 ViewController 的类更改为您在上面编写的代码的类。 u11 是否指定为 IBOutlet 并正确链接到 nib/storyboard?
  • 如果你对objective c非常陌生,最好使用第三个关于图像大小调整的库,例如github.com/mattgemmell/MGImageUtilities
  • u11 周围的视图层次结构是什么?也许不是 UIImageView 正在剪切图像,而是父视图?
  • u11 的帧大小是多少?其autresizesSubviews 属性是否设置为YES

标签: ios objective-c uiimageview uiimage


【解决方案1】:

你好,我会试试这个...

- (void)changeImages
{
    UIImage *img11 = [UIImage imageNamed@"dog.jpeg"];

    u11.contentMode = UIViewContentModeScaleAspectFit;
    u11.clipsToBounds = YES;
    [u11 setImage:img11];
}

- (void)viewWillAppear:animated
{
    [super viewWillAppear:animated];

    [self changeImages];
}

这将缩放图像(向上或向下),使其适合 imageView。拥有 clipsToBounds 不是必需的,但会阻止图像显示在 imageView 的框架之外。

HTH。

【讨论】:

  • 在 Swift 5 中 - UIViewContentModeScaleAspectFit 现在是“UIView.ContentMode.scaleAspectFit”,YES 现在是“true”。请注意,您可能希望使用 scaleAspectFill 而不是 scaleAspectFit,具体取决于场景。
【解决方案2】:
CGSize size=CGSizeMake(79, 84);//set the width and height
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();

这肯定有效,不要忘记导入 QuartzCore FrameWork..

祝你编码愉快 (^_^)....

【讨论】:

  • +1 表示 Quartz 方法。它在调整大小方面比标准的 UIKit contentMode 属性做得更好。
【解决方案3】:

添加到你的 UIViewController.m:

-(UIImage *)resizeImage:(UIImage *)image imageSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // here is the scaled image which has been changed to the size specified
    UIGraphicsEndImageContext();
    return newImage;
}

使用:

UIImage *image = [UIImage imageNamed:@"image.png"];
CGSize size = CGSizeMake(50, 63); // set the width and height 
UIImage *resizedImage = [self resizeImage:image imageSize:size];

我希望它有所帮助。

【讨论】:

【解决方案4】:

您还可以在 Interface Builder 的 Attributes Inspector 中将 View:Mode 设置为 Aspect Fit

【讨论】:

    【解决方案5】:

    我做了以下事情,它有帮助 将模式从默认值“Scale to fill”更改为“aspect fill”

    并添加一行代码如下(我是在单元格配置中完成的):

    cell.photo.clipsToBounds = true
    

    【讨论】:

      【解决方案6】:

      我知道这已经过时了,但我想根据斯坦福 193p 2017 第 11 课(大约 18:45)添加一个回复,并且对于任何快速查看的人来说,这是第一个显示给我的搜索结果。

      基本上,继承 UIView 并使其看起来像:

      class U11: UIView {
          var myImage: UIImage? { didSet { setNeedsDisplay() }}
          override func draw(_ rect: CGRect) {
              myImage?.draw(in: bounds)
          }
      }
      

      然后将图像设置为:

      func changeImage() {
          if let img11 = UIImage(named: "dog.jpeg"){
                  u11.myImage = img11
          }
      }
      

      这非常简单,图像占据了视图边界内的整个空间。

      【讨论】: