简体   繁体   English

UIImageView长宽比?

[英]UIImageView Aspect Ratio?

I have an image of size 320X460 and I want to create an UIImageView which height should be 450 . 我有一个大小为320X460的图像,我想创建一个UIImageView ,其高度应为450 To maintain aspect ratio I calculated the width of UIImageView = (320/460)*450 = 313.043 dynamically. 为了保持宽高比,我动态地计算了UIImageView =(320/460)* 450 = 313.043的宽度。 And set the contentMode For UIImageView is UIViewContentModeScaleAspectFit . 并设置contentMode为UIImageView是UIViewContentModeScaleAspectFit And set the image(320x460) to image view but it is some what blur. 并将图像(320x460)设置为图像视图,但是有些模糊。

Note: If I don't resize the UIImageView to 313.043X450 the image is very clear as it is. 注意:如果我不将UIImageView大小调整为313.043X450,则图像非常清晰。 So what is the mistake I have done? 那我犯了什么错误呢?

If I understand the question, this should answer it. 如果我理解这个问题,则应该回答它。

First to set the aspect ratio for the image in your image view. 首先在图像视图中设置图像的纵横比。

myView.contentMode = .scaleAspectFit

Next, your image may blur because it is a .png or another rasterized format. 接下来,您的图像可能会模糊,因为它是.png或其他光栅化格式。 You need to use .pdf as recommended by Apple or at very least another vectorized format. 您需要使用Apple推荐的 .pdf 或者至少使用另一种矢量化格式。 Rasterized images have values for all pixels in the image, so when the image is stretched too far it just duplicates and blurs pixels. 栅格化的图像具有图像中所有像素的值,因此,当图像拉伸得太远时,它只会复制并模糊像素。 Vectorized images do not blur because they are really just a series of instructions on how to draw/render the corresponding image. 矢量化图像不会模糊,因为它们实际上只是一系列有关如何绘制/渲染相应图像的指令。 在此处输入图片说明

If you are resizing UIImageView manually, set the content mode to UIViewContentModeScaleAspectFill . 如果要手动调整UIImageView的大小,则将内容模式设置为UIViewContentModeScaleAspectFill

If you want to keep content mode UIViewContentModeScaleAspectFit do not resize imageview to 313. Image will adjust maximum possible width and height , keeping it's aspect ratio. 如果要保持内容模式UIViewContentModeScaleAspectFit请不要将imageview的大小调整为313。图像将调整最大可能的宽度和高度,并保持其纵横比。

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

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