-3

An image is set on an imageView. In assets, i have 1x 2x and 3x images for that.How I know that particular device is picking 1x or 2x or 3x .There is any way to check it programatically in ios using swift.

UIScreen.main.scale Is way to getting that particular device is picking 1x or 2x or 3x according to its scale. but truly , it is not behave like that . iPhone 6s+ plus is not picking 3x. always get blurred image.

Rajesh Sharma
  • 99
  • 2
  • 10

3 Answers3

9

Asset images are picked according to device screen's scale factor. To get screen's scale, you can use this:

UIScreen.main.scale // returns CGFloat
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • According to UIScreen.main.scale, iphone6s+ scale is 3.0, so iphone6s+ need to pick an image of 3x . But I got blurred image on imageView. My imageView frame is 90*90. I have an image 1x Size - 30*30 , 2x Size - 60*60 3x Size - 90*90 but imageView not pick 3x. – Rajesh Sharma Jun 06 '18 at 10:37
  • 3
    If your imageView frame is 90 x 90, then your asset images should be 90 x 90 for 1x, 180 x 180 for 2x, and 270 x 270 for 3x. – vacawama Jun 06 '18 at 10:43
  • @RajeshSharma: Generally speaking set the frame of your imageview similar to 1x image. In case you are using autolayout, you dont need to give width/height to UIImageView, it will resize itself according to the content. You can see this in interface builder where you can give the image name and it will resize UIImageview according to image size. – Puneet Sharma Jun 06 '18 at 11:13
2

You don't need to select the images manually, the system will use the image with the appropriate scale based on the device.

However, from comments it seems like you misunderstood how logical resolutions work. You need images of size physical resolution * scale. So for an imageView of size 90x90, 1x is 90x90, 2x is 180x180, 3x is 270x270 in your case.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 1
    And please don't just scale up the 90x90 image in Photoshop. You need to start with a nice sharp 270x270 image and scale down to create the others. – vacawama Jun 06 '18 at 10:47
0

The following answer for a different question explains the image selection mechanism.
It also has a table that connects device type to the selected image (1x/2x/3x)

Eyal.K
  • 431
  • 6
  • 14