1

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?

jailani
  • 2,260
  • 2
  • 21
  • 45
  • 1
    don't set the content-mode for the UIImageView if you are resizing it – Ratikanta Patra Apr 28 '14 at 10:24
  • 1
    also, try to make the sizes ints. Simply use round: round(313.043) – EsbenB Apr 28 '14 at 10:25
  • 1
    @RatikantaPatra. I am not resizing the uiimage I am resizing the UIImageView so If I don't set content mode It will shrink... – jailani Apr 28 '14 at 10:28
  • @EsbenB That is not solution i think... – jailani Apr 28 '14 at 10:29
  • 1
    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 pixels – EsbenB Apr 28 '14 at 10:35
  • @EsbenB Please refer jackson's answer with picture representation in the following post: http://stackoverflow.com/questions/185652/how-to-scale-a-uiimageview-proportionally – jailani Apr 28 '14 at 10:39
  • lol. anyways... provide related screenshots (before and after resizing the `UImageView`) and include it in your question so we get a better idea of what is happening and what you expect. – staticVoidMan Apr 28 '14 at 10:42
  • @IOSDeveloper don't set he contentMode property at all. It takes the default value UIViewContentModeScaleToFill. Simple set the image i.e imageView.image=yourImage. That should do – Ratikanta Patra Apr 28 '14 at 11:27

2 Answers2

1

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

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
0

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.

Rukshan
  • 7,902
  • 6
  • 43
  • 61