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

查看:178
本文介绍了如何使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);

但是,这不会更改其中包含的视图的布局,您可以使用Layout Constraints使它在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.

如果您认为视图不会增长,并且问题在于将图像更改为与包含视图的尺寸不匹配的图像时,图像如何溢出边界,则可以设置剪切子视图"图像视图的界面生成器中的复选框.这样一来,视图就不会在其自己的边界框外绘制任何内容,如果您还将缩放模式设置为纵横填满"或缩放填满",则图像将始终填满包含视图的整个边界

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天全站免登陆