简体   繁体   English

Xcode 6 UIImageView无法正确缩放

[英]Xcode 6 UIImageView will not scale correctly

Please see screen shot of flower setup below. 请在下面查看鲜花设置的屏幕截图。 The flower image has been correctly loaded from an asset catalog and when the app is run on various simulators the correct pixel resolution is assigned to each device. 花卉图片已从资产目录中正确加载,并且在各种模拟器上运行该应用程序时,已为每个设备分配了正确的像素分辨率。 My problem is how to get the flower image to be scaled (equally sized to fit) the same on each device ?? 我的问题是如何使花朵图像在每个设备上都可以缩放(相等大小以适合)? I have learnt how to position the image to different positions using constraints and frames but the image never scales correctly - please see first pic 我已经学习了如何使用约束和框架将图像定位到不同的位置,但是图像无法正确缩放-请参阅第一个图片 在此处输入图片说明

The following image is a mock up of what I want to be able to do (flower image scaled correctly on each device) 下图是我想做的事的模拟照片(在每台设备上正确缩放的花朵图像)

在此处输入图片说明

Judging by your mockups, it looks like you want the image to fill half the width, and keep its square aspect ratio to determine its height. 从样机来看,您似乎希望图像填充一半的宽度,并保持其正方形纵横比来确定其高度。 One way to approach this would be to use AutoLayout to make a left UIImageView and a right placeholder (blank) view. 解决此问题的一种方法是使用AutoLayout创建左UIImageView和右占位符(空白)视图。 Pin the left view to the left edge of the parent, the right view to the right edge, and then set them to be 0 pixels from each other. 将左视图固定到父对象的左边缘,将右视图固定到右边缘,然后将它们彼此设置为0像素。 Then set an equal widths constraint on them. 然后对其设置相等的宽度约束。 Finally, control drag the image view to itself and you can select aspect ratio -- and assuming that in IB, the width and height is the same, it will keep it square. 最后,控件将图像视图拖到其自身,然后可以选择宽高比-并假设在IB中,宽度和高度相同,它将保持正方形。 Adding an equal heights constraint will give the other view the height it needs to be equal in case you need that. 添加相等的高度约束将为另一个视图提供相等的高度,以备您需要。

在此处输入图片说明

在此处输入图片说明占位符视图约束

This gives you a left image view that is 50% and with your mode set to Aspect Fit or Aspect Fill, it should give you the results in your mockups. 这将为您提供50%的左图像视图,并且您的模式设置为Aspect Fit或Aspect Fill,它应该为您的模型提供结果。 In case you have an image that isn't square, make sure to check Clip Subviews for your UIImageView to prevent showing the overflow. 如果您的图像不是正方形,请确保检查UIImageView的Clip Subviews以防止显示溢出。

The real problem is that your goals are not well defined. 真正的问题是您的目标定义不明确。 Scaled with respect to what ? 关于什么规模? The screen has a height and a width. 屏幕具有高度和宽度。 You can't scale with respect to both, because that would distort the image (because different devices have different overall aspect ratios). 您无法同时缩放两者,因为这会使图像变形(因为不同的设备具有不同的总体纵横比)。 Thus you have to pick one dimension that you will scale to. 因此,您必须选择一个要缩放的尺寸。

Judging from your mockup, I'm going to guess that what you want is to scale with respect to height . 从您的模型判断,我将猜测您想要的是相对于height进行缩放。 If so, then give the image view a height constraint that is a fixed fraction of its superview's height. 如果是这样,则给图像视图一个高度限制,该限制是其超级视图高度的固定分数。 It looks to be about 0.25 in your mockups but you will have to eyeball what you think is a good fraction. 在您的模型中看起来大约0.25 ,但您将不得不认真对待您认为是好的部分。

Now, if the content mode of the image view is Aspect Fit (or Aspect Fill), the image will change its height along with the image view without distorting the aspect ratio. 现在,如果图像视图的内容模式是“宽高比适合”(或“长宽比填充”),则图像将随图像视图一起更改其高度,而不会扭曲宽高比。

However, it would be best if you would make the other dimension (here, the width) of the image view a fixed fraction of the height, such as to make the aspect ratio of the image view the same as the aspect ratio of the image. 但是,最好将图像视图的其他尺寸(此处为宽度)设置为高度的固定比例,例如使图像视图的长宽比与图像的长宽比相同。 The reason is that otherwise the image might end up centered in the image view in such a way that it doesn't touch the top or left any more, even though the image view itself does. 原因是,否则图像可能最终以这样的方式最终居中放置在图像视图中:即使图像视图本身也不会碰到顶部或不再离开。

CGFloat screen_width = [[UIScreen mainScreen] bounds].size.width;
your_imageview.frame = CGRectMake(0, 0, screen_width/2, screen_width/2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM