IOS image from Retina Size URL?

I would like to download some icons from Webservice for different sizes (1x, 2x and 3x) - but when there is no image name with x2 at the end, ios always uses it as 1x.

I already found some answers that you must specify a url with imagename@2x.png - so that iOS can recognize a Retina image, but how should it work correctly with deleted images for three sizes?

In my case, the Webservice, which can provide images of all sizes, turned out badly. I would like to specify the correct size for the device with url.

For example:

http://example.com/x2/image.png

or

http://example.com/x3/image.png

I usually use Images.xcassets poorly to provide all different sizes, but this time I would like to upload images remotely. How to check which size is suitable for your device? Do I have to request a screen resolution (or for iPhone Type?) To check which image should load?

And how can I tell UIImageView that the image coming from this URL:

http://example.com/x3/image.png

equal to 3x (so do not increase it 3 times)?

Thank you in advance

+4
source share
4 answers

Use [UIScreen mainScreen].scaleto find out what size you will need. And then load the image using the the API: + (nullable UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale. You can specify what image scale.

+3

[UIScreen mainScreen].scale;

.

:

 UIScreen.mainScreen().scale

:

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0)){
     // Retina display
}else{
    // non-Retina display
}
0

You can define some macros, such as

    #define iPad    UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

    #define iPhone5OrBelow ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height < 667)

    #define iPhone6 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 667)

    #define iPhone6Plus ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 736)

Than you can check any of these macros, for example,

if (iPhone6 || iPhone5OrBelow)
{
 // use @2ximage.png
}
else 
{
 // use @3ximage.png
}

Hope this helps.

0
source
You can check for iPhone.

For Ex :
if(iPHone5 || iPhone5S || iPhone6 || iPhone6S){
// load 2x images.
  use this link http://example.com/x2/image.png
  yourImgView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:yourLinkfor2ximage]];

  }
  else if(iPhone6+ || iPhone6+S ){
  //use this link http://example.com/x3/image.png
    yourImgView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:yourLinkfor3ximage]];
  }
-3
source

Source: https://habr.com/ru/post/1620714/


All Articles