更改 UIImageView 大小以匹配图像与 AutoLayout [英] Change UIImageView size to match image with AutoLayout

查看:18
本文介绍了更改 UIImageView 大小以匹配图像与 AutoLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改我的 UIImageView 高度/宽度以匹配我从相册中选择的图像,我在这里找到了一个片段.

I want to change my UIImageView height/width to match the image i choose from photo album and i found a snippet on here that does that.

-(CGRect)frameForImage:(UIImage *)imgs inImageViewAspectFit:(UIImageView *)imagev
{
  float imageRatio = imgs.size.width / imgs.size.height;
  float viewRatio = imagev.frame.size.width / imagev.frame.size.height;
  if(imageRatio < viewRatio)
  {
    float scale = imagev.frame.size.height / imgs.size.height;
    float width = scale * imgs.size.width;
    float topLeftX = (imagev.frame.size.width - width) * 0.5;
    NSLog(@"image after aspect fit: width=%f",width);
    imagev.frame = CGRectMake(topLeftX, 0, width, imagev.frame.size.height);
    return CGRectMake(topLeftX, 0, width, imagev.frame.size.height);
  }
  else
  {
    float scale = imagev.frame.size.width / imgs.size.width;
    float height = scale * imgs.size.height;
    float topLeftY = (imagev.frame.size.height - height) * 0.5;
    NSLog(@"image after aspect fit: height=%f", height);
    imagev.frame = CGRectMake(0, topLeftY, imagev.frame.size.width, height);
    return CGRectMake(0, topLeftY, imagev.frame.size.width, height);
  }
}

问题是当我从按钮调用它时它会改变 UIImageView 的高度/宽度

The problem is that it changes the UIImageView height/width when i call it from a button

- (IBAction)changeSize:(id)sender
{
  [self frameForImage:image inImageViewAspectFit:self.imageView];    
}

但我希望它在我从相册中选择图像后立即执行此操作,这就是我设置它的方式.

But i want it to do that as soon as i choose a image from photo album and this is how i have it set up.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  image = [info objectForKey:UIImagePickerControllerOriginalImage];
  [self.imageView setImage:image];
  [self frameForImage:image inImageViewAspectFit:self.imageView];
  [self dismissViewControllerAnimated:YES completion:NULL];
}

推荐答案

自动布局解决方案

自从确定您在项目中使用自动布局后,我制作了一个演示应用程序来向您展示如何更改图像视图的图像并调整高度.自动布局会自动为您执行此操作,但要注意的是,您将使用的照片来自用户图库,因此它们可能非常大而且这个.

Auto Layout solution

Since establishing that you're using Auto Layout in your project, I have made a demo app to show you how you could change the image of the image view and adjust the height. Auto Layout will do this for you automatically, but the catch is that the photo you'll be using is coming from the users gallery and so they're likely to be very big and this.

请查看应用程序:https://bitbucket.org/danielphillips/auto-layout-图像视图

诀窍是创建图像视图高度的 NSLayoutConstraint 的引用.当您更改图像时,您需要在给定宽度的情况下将其常量调整为正确的高度.

The trick is to create a reference of the NSLayoutConstraint of the height of the image view. When you change your image, you need to adjust it's constant to the correct height given the fixed width.

您的其他解决方案可能是在您的图像视图上设置 contentMode,通过使用 UIViewContentModeScaleAspectFit 您的图像将始终完整显示,但将锁定在图像视图,它可以根据您拥有的自动布局约束而改变.

Your other solution could be to set a contentMode on your image view, by using UIViewContentModeScaleAspectFit your image will always appear in full but will be locked to the bounds of the image view, which can change based on the Auto Layout constraints you have.

看起来你真的把这件事弄得太复杂了,除非我错过了什么.

It looks like you've really over complicated this, unless I've missed something.

当你从图像选择器中获取新图像时,你需要做的就是根据 UIImage 大小改变图像视图的框架.

When you get a new image from the image picker, all you need to do is change the frame of the image view according to the UIImage size.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  image = [info objectForKey:UIImagePickerControllerOriginalImage];
  [self.imageView setImage:image];
  self.imageView.frame = CGRectMake(self.imageView.frame.origin.x
                                    self.imageView.frame.origin.y
                                    image.size.width
                                    image.size.height);
  [self dismissViewControllerAnimated:YES completion:NULL];
}

当然,这可能会非常大(它使用的原始图像可能非常大).

Of course this is going to be potentially very large (it's using the original image which may be very large).

所以让我们将宽度锁定为 280 点...这样我们就可以在纵向模式下始终在屏幕上显示完整的图像.

So let's lock the width to 280 points... this way we can always have the full image on screen in portrait mode.

所以假设您的图像尺寸是 1000x800,而我们的图像视图可能是 280x100.我们可以像这样计算保留图像视图宽度的图像视图的正确高度:

So assuming your image size is 1000x800, and our image view perhaps is 280x100. We can calculate the correct height for the image view retaining the image view's width like this:

CGSize imageSize        = CGSizeMake(1000.0, 800.0);
CGSize imageViewSize    = CGSizeMake(280.0, 100.0);

CGFloat correctImageViewHeight = (imageViewSize.width / imageSize.width) * imageSize.height;

self.imageView.frame = CGRectMake(  self.imageView.frame.origin.x,
                                    self.imageView.frame.origin.x,
                                    CGRectGetWidth(self.imageView.bounds),
                                    correctImageViewHeight);

这篇关于更改 UIImageView 大小以匹配图像与 AutoLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆