ios UIImageView:将大小更改为图像大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8701751/
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
UIImageView: Change size to image size?
提问by dhrm
I've an UIImageView
with content mode Aspect Fitof size 220x155. I'm dynamically inserting different images in different resolutions, but all larger than the size of the UIImageView. As the content mode is set to Aspect Fit, the image is scaled with respect to the ratio to fit the UIImageView.
我有一个大小为 220x155 的UIImageView
内容模式Aspect Fit。我以不同的分辨率动态插入不同的图像,但都大于 UIImageView 的大小。当内容模式设置为Aspect Fit 时,图像会根据比例缩放以适合 UIImageView。
My problem is, that if for instance the image inside the UIImageView is scaled to 220x100, I would like the UIImageView to shrink from a height of 155 to 100 too to avoid space between my elements.
我的问题是,例如,如果 UIImageView 内的图像缩放到 220x100,我希望 UIImageView 也从 155 的高度缩小到 100 以避免我的元素之间出现空间。
How can I do this?
我怎样才能做到这一点?
回答by Micha? Zygar
If I got you right, it would be something like this: get image size by:
如果我猜对了,它会是这样的:通过以下方式获取图像大小:
UIImage * img = [UIImage imageNamed:@"someImage.png"];
CGSize imgSize = img.size;
calculate scale ratio on width
计算宽度的比例
float ratio=yourImageView.frame.size.width/imgSize.width;
check scaled height (using same ratio to keep aspect)
检查缩放高度(使用相同的比例保持纵横比)
float scaledHeight=imgSize.height*ratio;
if(scaledHeight < yourImageView.frame.size.height)
{
//update height of your imageView frame with scaledHeight
}
回答by Steven Stefanik
Based on Michael's answer, here's a complete method
根据迈克尔的回答,这是一个完整的方法
+ (CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
float widthScale = 0;
float heightScale = 0;
widthScale = boxSize.width/originalSize.width;
heightScale = boxSize.height/originalSize.height;
float scale = MIN(widthScale, heightScale);
CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
return newSize;
}
回答by mrplants
Edit for Steven Stefanik's answer: This method breaks when originalSize is {0, 0}. Maybe consider these changes
编辑 Steven Stefanik 的回答:当 originalSize 为 {0, 0} 时,此方法会中断。也许考虑这些变化
-(CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize
{
if (originalSize.height == 0) {
originalSize.height = boxSize.height;
}
if (originalSize.width == 0) {
originalSize.width = boxSize.width;
}
float widthScale = 0;
float heightScale = 0;
widthScale = boxSize.width/originalSize.width;
heightScale = boxSize.height/originalSize.height;
float scale = MIN(widthScale, heightScale);
CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
return newSize;
}
回答by Ignacio Inglese
Last edit:
最后编辑:
It seems I misunderstood the question.
看来我误解了这个问题。
To get the actual size you could try:
要获得实际尺寸,您可以尝试:
CGSize imageSize = img.size;
CGSize viewSize = imageView.frame.size;
CGSize actualSize;
if (imageSize.width > imageSize.height) {
actualSize.width = imageSize.width > viewSize.width ? viewSize.width : imageSize.width;
actualSize.height = imageSize.height * actualSize.width / imageSize.width;
}
else {
actualSize.height = imageSize.height > viewSize.height ? viewSize.height : imageSize.height;
actualSize.width = imageSize.width * actualSize.height / imageSize.height;
}