更改的UIImageView大小与自动版式形象相匹配 [英] Change UIImageView size to match image with AutoLayout

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

问题描述

我想改变我的UIImageView的高度/宽度我从相册选择图像匹配,我发现了一个片段在这里做到这一点。

   - (的CGRect)frameForImage:(*的UIImage)IMGS inImageViewAspectFit:(UIImageView的*)imagev
{
  浮imageRatio = imgs.size.width / imgs.size.height;
  浮viewRatio = imagev.frame.size.width / imagev.frame.size.height;
  如果(imageRatio< viewRatio)
  {
    浮规模= imagev.frame.size.height / imgs.size.height;
    浮宽度=规模* imgs.size.width;
    浮动topLeftX =(imagev.frame.size.width - 宽)* 0.5;
    的NSLog(@后形象方面配合:宽=%F,宽度);
    imagev.frame = CGRectMake(topLeftX,0,宽度,imagev.frame.size.height);
    返回CGRectMake(topLeftX,0,宽度,imagev.frame.size.height);
  }
  其他
  {
    浮规模= imagev.frame.size.width / imgs.size.width;
    浮球高度=规模* imgs.size.height;
    浮动topLeftY =(imagev.frame.size.height - 高)* 0.5;
    的NSLog(@后形象方面配合:高=%F,高度);
    imagev.frame = CGRectMake(0,topLeftY,imagev.frame.size.width,高度);
    返回CGRectMake(0,topLeftY,imagev.frame.size.width,高度);
  }
}

问题是,它改变了UIImageView的高度/宽度,当我把它从一个按钮

   - (IBAction为)changeSize:(ID)发送者
{
  [自我frameForImage:形象inImageViewAspectFit:self.imageView];
}

但我想我选择相册一画像尽快做到这一点,这是我有它设置。

   - (无效)imagePickerController:(*的UIImagePickerController)选择器didFinishPickingMediaWithInfo:(NSDictionary的*)信息
{
  图像= [信息objectForKey:UIImagePickerControllerOriginalImage];
  [self.imageView setImage:图像]。
  [自我frameForImage:形象inImageViewAspectFit:self.imageView];
  [个体经营dismissViewControllerAnimated:YES完成:NULL];
}


解决方案

自动布局解决方案

公司自成立以来,你在你的项目中使用自动布局,我做了一个演示程序,以显示你如何可以改变图像视图的形象和调节高度。自动布局将自动为您做到这一点,但美中不足的是,你要使用的照片是从用户的画廊来了,所以他们可能会非常大,这一点。

所以检查出的应用程序:<一href=\"https://bitbucket.org/danielphillips/auto-layout-imageview\">https://bitbucket.org/danielphillips/auto-layout-imageview

关键是要创造的形象观的高度的 NSLayoutConstraint 的参考。当你改变你的形象,你需要调整它的常数正确的高度给出的固定宽度。

您其他的解决办法是设置一个 contentMode 你的图像视图,通过使用 UIViewContentModeScaleAspectFit 你的形象将永远出现在全,但会被锁定到图像视图的边界,它可以改变的基础上,你有自动布局约束。

初步答复

看起来你真的在这复杂的,除非我已经错过了一些东西。

当你从图像选择器一个新的形象,所有你需要做的是根据的UIImage 尺寸改变图像视图的框架。

   - (无效)imagePickerController:(*的UIImagePickerController)选择器didFinishPickingMediaWithInfo:(NSDictionary的*)信息
{
  图像= [信息objectForKey:UIImagePickerControllerOriginalImage];
  [self.imageView setImage:图像]。
  self.imageView.frame = CGRectMake(self.imageView.frame.origin.x
                                    self.imageView.frame.origin.y
                                    image.size.width
                                    image.size.height);
  [个体经营dismissViewControllerAnimated:YES完成:NULL];
}

当然,这将是潜在的非常大的(它使用的可能是非常大的原始图像)。

因此​​,让我们锁定宽度280点......这样,我们总是可以在屏幕上完整的图像在纵向模式。

所以假设你的影像尺寸为1000x800,而我们的图像视图也许是280x100。我们可以计算出图像显示正确的高度保持图像视图的宽度是这样的:

  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);

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);
  }
}

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.

So check out the app: https://bitbucket.org/danielphillips/auto-layout-imageview

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.

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.

Initial reply

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

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).

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

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大小与自动版式形象相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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