简体   繁体   English

如何将UIImageView限制为最佳大小?

[英]How to constrain a UIImageView to the best size?

In my IOS app I want to display the biggest variation of an image that can be fully displayed on the device. 在我的IOS应用中,我希望显示可以完全显示在设备上的图像的最大变化。 I want to do it without resizing the image to be larger or smaller so that the image doesn't seem blurry. 我想做到这一点而不必将图像调整为更大或更小,以使图像看起来不会模糊。

On my webserver I have 3 variations of the same image. 在我的网络服务器上,我有3张相同图片的变体。 To keep it simple let's say they only differ in width: 640, 750 and 1000. 为了简单起见,假设它们的宽度不同:640、750和1000。

An iPhone 5 will display the 640 width image. iPhone 5将显示640宽度的图像。 An iPhone 6 will display the 750 width image. iPhone 6将显示750宽度的图像。 An iPhone 6+ will display the 1000 width image. iPhone 6+将显示1000宽度的图像。

I can easily get the size of a UIImageView in order to download the right image, but how to do I set an Auto Layout Constraint on the UIImageView that sets the UIImageView to the right width? 我可以轻松获取UIImageView的大小以下载正确的图像,但是如何在UIImageView上设置自动布局约束以将UIImageView设置为正确的宽度?

If I set a low priority <= 1000 width constraint on the UIImageView, then the image is displayed at a width of 1000 regardless of the size of the device. 如果我在UIImageView上设置了一个低优先级<= 1000宽度约束,那么无论设备大小如何,图像都以1000的宽度显示。 This is true even when I set the UIImageView to have a low Content Compression Resistance Priority. 即使我将UIImageView设置为较低的“内容压缩抗性”优先级,也是如此。

I am using XCode, Objective C and IOS 8. 我正在使用XCode,Objective C和IOS 8。

If I'm going about this wrong please tell me. 如果我要解决这个错误,请告诉我。

Thank you! 谢谢!

easy solution would be to make a width constraint of the UIImageView, then create an outlet for the same. 一个简单的解决方案是对UIImageView进行宽度限制,然后为其创建一个出口。

Now inside the code , set constraint.constant value by calculating according to the device. 现在在代码内部,通过根据设备进行计算来设置constraint.constant值。

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

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