更改UIImageView大小以使图像与AutoLayout匹配

我想改变我的UIImageView的高度/宽度,以匹配我从相册中select的图像,我在这里find了一个片段,这样做。

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

问题是,当我从一个button调用它时,它改变了UIImageView的高度/宽度

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

但是我希望这样做,只要我从相册中select一个图像,这是我如何设置。

 -(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]; } 

自动布局解决scheme

由于确定您在项目中使用了“自动布局”,因此我制作了一个演示应用程序,向您展示如何更改图像视图的图像并调整高度。 自动布局会自动为您做这个,但问题是您要使用的照片来自用户库,所以它们可能会非常大,

所以看看应用程序: https : //bitbucket.org/danielphillips/auto-layout-imageview

诀窍是创build图像视图高度的NSLayoutConstraint的引用。 当你改变你的图像时,你需要调整它恒定的高度给定的宽度。

您的其他解决scheme可能是在您的图像视图上设置contentMode ,通过使用UIViewContentModeScaleAspectFit您的图像将始终显示完整,但将被locking到图像视图的边界,它可以根据您的自动布局约束来更改。

初始答复

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

当您从图像select器获取新图像时,只需根据UIImage大小更改图像视图的框架即可。

 - (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]; } 

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

所以我们把宽度locking到280点…这样我们就可以在肖像模式下始终在屏幕上显示完整的图像。

所以假设你的图像尺寸是1000×800,我们的图像视图可能是280×100。 我们可以为保留图像视图宽度的图像视图计算正确的高度,如下所示:

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

这是一个Swift类,它将帮助您根据您select的宽度或高度限制返回图像的正确大小:

 class func returnImageSizeForHeightLimit(heightLimit:CGFloat?, orWidthLimit widthLimit:CGFloat?, forImage theImage:UIImage)->CGSize?{ if let myHeightLimit = heightLimit { let theWidth = (myHeightLimit/theImage.size.height) * theImage.size.width return CGSizeMake(theWidth, myHeightLimit) } if let myWidthLimit = widthLimit { let theHeight = (myWidthLimit/theImage.size.width) * theImage.size.height return CGSizeMake(myWidthLimit, theHeight) } return nil }