ios 如何让 UIImageView 自动调整到加载图片的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12603025/
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
How to make UIImageView automatically resize to the size of the image loaded
提问by Louis Shraga
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 imageproperty 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 ?
我用 IB 在我的视图上放置了一个 UIImageView 控件。控件的大小只是我决定的,非常随机的大小我想要做的是每当我将图像属性设置为新图像时自动调整大小的控件。我希望它实际调整到图像的大小。可以自动完成吗?没有任何代码干预?如果不是 - 在这种情况下最好的方法是什么?
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 ?
今天发生的事情很奇怪。我将图像加载到 ImageView 中,即使 ImageView 的大小没有改变,我也看到图像得到正确显示。这干扰了我抓住用户触摸 ImageView 的意图。用户触摸实际图像,但由于图像的某些部分在 ImageView 之外(这是奇怪的部分)-点映射变得疯狂有人能想到对此的任何解释吗?
thanks
谢谢
回答by Hampus Nilsson
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.
图像的大小与 UIImageView 的实际大小无关,而 UIImageView 的大小仅取决于 Interface Builder 中给定的大小(或您分配给它的大小)。否则,例如,当您将 @2x 图像用于 Retina 显示器时,图像会很奇怪。
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"]];
change it to:
将其更改为:
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);
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.
然而,这不会改变它所包含的视图的布局,您可以使用布局约束使其在 iOS 6 下自动更改其他视图的大小。如果您是 Apple 开发人员,您可以观看 WWDC 说明视频,它们解释了该系统如何工作得很好。
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.
如果您对视图没有增长感到满意,并且问题在于当您将其更改为与包含视图的尺寸不匹配的图像时图像如何溢出其边界,则可以在界面中设置“剪辑子视图”复选框图像视图的构建器。这将使视图不会在其自己的边界框之外绘制任何内容,如果您还将缩放模式设置为“Aspect Fill”或“Scale To Fill”,则图像将始终填充包含视图的整个边界.
回答by J-Dizzle
Here is the code snippet I cut and paste often, using the same method as Hampus Nilsson above -
这是我经常剪切和粘贴的代码片段,使用与上面的 Hampus Nilsson 相同的方法 -
Example
例子
func demo(x0:CGFloat, y0:CGFloat) {
let imgView = UIImageView(image: UIImage(named: "someImg.png"));
imgView.sizeToImage();
imgView.center = CGPoint(x:x0, y:y0);
}
UIImageView Extension
UIImageView 扩展
extension UIImageView {
/******************************************************************************/
/** @fcn sizeToImage()
* @brief size view to image
* @@assum (image!=nil)
*/
/******************************************************************************/
func sizeToImage() {
//Grab loc
let xC = self.center.x;
let yC = self.center.y;
//Size to fit
self.frame = CGRect (x: 0, y: 0, width: (self.image?.size.width)!/2, height: (self.image?.size.height)!/2);
//Move to loc
self.center = CGPoint(x:xC, y:yC);
return;
}
A wonderful cut & paste, use if needed!
一个美妙的剪切和粘贴,如果需要使用!