1
votes

I have an image of size 320X460 and I want to create an UIImageView which height should be 450. To maintain aspect ratio I calculated the width of UIImageView = (320/460)*450 = 313.043 dynamically. And set the contentMode For UIImageView is UIViewContentModeScaleAspectFit. And set the image(320x460) to image view but it is some what blur.

Note: If I don't resize the UIImageView to 313.043X450 the image is very clear as it is. So what is the mistake I have done?

2
don't set the content-mode for the UIImageView if you are resizing itRatikanta Patra
also, try to make the sizes ints. Simply use round: round(313.043)EsbenB
@RatikantaPatra. I am not resizing the uiimage I am resizing the UIImageView so If I don't set content mode It will shrink...jailani
@EsbenB That is not solution i think...jailani
RatikantaPatra suggests that you set the contentMode to UIViewContentModeScaleToFill. When you resize an image there is always of risk of blurring the image. In this case the UIimageView needs to "compress" a few pixelsEsbenB

2 Answers

1
votes

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. You need to use .pdf as recommended by Apple or at very least another vectorized format. 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. enter image description here

0
votes

If you are resizing UIImageView manually, set the content mode to 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.