ios 更改 UIImageView 大小以将图像与 AutoLayout 匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16878607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change UIImageView size to match image with AutoLayout
提问by Mike Tyson
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.
我想更改我的 UIImageView 高度/宽度以匹配我从相册中选择的图像,我在这里找到了一个片段。
-(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
问题是当我从按钮调用它时它会改变 UIImageView 的高度/宽度
- (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];
}
回答by Daniel
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
所以看看这个应用程序: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.
诀窍是创建NSLayoutConstraint
图像视图高度的参考。当您更改图像时,您需要在给定宽度的情况下将其调整为正确的高度。
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.
您的其他解决方案可能是contentMode
在您的图像视图上设置 a ,通过使用UIViewContentModeScaleAspectFit
您的图像将始终完整显示,但将被锁定到图像视图的边界,这可以根据您拥有的自动布局约束进行更改。
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.
当您从图像选择器中获取新图像时,您需要做的就是根据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];
}
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.
因此,让我们将宽度锁定为 280 磅……这样我们就可以在纵向模式下始终在屏幕上显示完整图像。
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:
所以假设你的图像大小是 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);
回答by PJeremyMalouf
Here is a Swift class that will help return the correct sizing for an image based on a width or height limit that you choose:
这是一个 Swift 类,它将帮助根据您选择的宽度或高度限制返回图像的正确大小:
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
}