如何使 UIImageView 自动调整为加载图像的大小 [英] How to make UIImageView automatically resize to the size of the image loaded

查看:29
本文介绍了如何使 UIImageView 自动调整为加载图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 IB 在我的视图上放置了一个 UIImageView 控件.控件的大小只是我决定的,非常随机的大小我想要做的是每当我将 image 属性设置为新图像时自动调整大小的控件.我希望它实际调整为图像的大小.可以自动完成吗?没有任何代码干预?如果不是 - 在这种情况下最好的方法是什么?

I have put an UIImageView control on my view with IB. The size of the control is just something I decided upon, pretty random size really What I want to do is the control to resize automatically whenever I set the image property to a new image. I want it to actually resize to the size of the image. Can it be done automatically ? without any code intervention ? If not - what will the best approach be in this case ?

今天发生的事情很奇怪.我将图像加载到 ImageView 中,即使 ImageView 的大小没有改变,我也看到图像正确显示.这干扰了我在 ImageView 上抓取用户触摸的意图.用户触摸实际图像,但由于图像的某些部分在 ImageView 的外部(这是奇怪的部分) - 点映射变得疯狂有人能想到任何解释吗?

What happens today is strange. I load images into the ImageView and I see the images getting displayed properly even though the size of the ImageView is not changed. This interferes with my intention of grabbing users touches over the ImageView. The user touches the actual image, but since some parts of the image are outside ( and this is the strange part ) of the ImageView - point mapping goes crazy Can someone think of any explanation to this ?

谢谢

推荐答案

图像的大小与 UIImageView 的实际大小无关,UIImageView 的大小仅取决于在 Interface Builder 中赋予它的大小(或您分配给它的).否则,例如,当您将 @2x 图像用于 Retina 显示器时,图像会很奇怪.

The size of an image has no bearing on how large the UIImageView actually is, rather the size of the UIImageView solely depends on the size given to it in Interface Builder (or that you assigned to it). Else the images would be all whacky when you use the @2x images for Retina displays for example.

如果你想解决这个问题,你也必须在设置图像时更改框架.如果你现在这样做:

If you want to fix this, you must change the frame when setting the image as well. If you're doing this now:

[imageView setImage:[UIImage imageNamed:@"myImage.jpg"]];

改成:

UIImage img = [UIImage imageNamed:@"myImage.jpg"];
[imageView setImage:img];
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,
                             img.size.width, img.size.height);

然而,这不会改变它所包含的视图的布局,你可以让它在 iOS 6 下使用布局约束自动改变其他视图的大小.如果您是 Apple 开发人员,您可以观看 WWDC 说明视频,它们会解释该系统如何运作良好.

This will however not change the layout of view it is contained within, you can make it change the sizes of the other views automatically under iOS 6 using Layout Constraints. If you are an Apple Developer you can watch the WWDC instruction videos, they explain how that system works quite well.

如果您对视图不增长感到满意,而问题是当您将其更改为与包含视图的尺寸不匹配的图像时,图像如何溢出边界,您可以设置剪辑子视图"Interface Builder 中用于图像视图的复选框.这将使视图不会在它自己的边界框之外绘制任何东西,如果您还将缩放模式设置为Aspect Fill"或Scale To Fill",图像将始终填满包含视图的整个边界.

If you're fine with the view not growing, and the problem is just how the image overflows it's bounds when you change it to one that does not match the dimension of the containing view, you can set the "Clip Subviews" checkbox in Interface Builder for the image view. This will make it so that the view will not draw anything outside it's own bounding box, if you also set the scaling mode to "Aspect Fill" or "Scale To Fill", the image will always fill up the entire bounds of the containing view.

这篇关于如何使 UIImageView 自动调整为加载图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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