How to load corresponding Default.png in UImageView?

I have default pop-up screens with the names: Default-568h@2x.png , Default-Portrait.png, Default.png, Default@2x.png , etc. For all types of devices.

I know that the system automatically selects the appropriate splash screen for a specific device and displays it.

Questions: is it possible to find out which image is selected by the system? How to load the corresponding image selected by the system in UIimageView.

I tried this:

UIImageView *splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)]; splashView.image=[UIImage imageNamed:@"Default.png"]; 

But it only downloads an image named Default.png for all types of devices (iPhone 4, 5, iPad).

Do I need to manage this manually? I want to download the corresponding image after identifying the device type?

+4
source share
4 answers

I found this question, having run into the same problem. It seems that if you use [UIImage imagedNamed:@"Default"]; , iOS will detect the retina against non-retina and apply @2x , but will not detect iPhone 5 and apply -568h

The solution I came up with was to write a category in UIImage that checks the height of the main window and returns the corresponding image if it exists:

 @interface UIImage (Compatible) + (UIImage *)compatibleImageNamed:(NSString *)name; @end @implementation UIImage (Compatible) + (UIImage *)compatibleImageNamed:(NSString *)name { if ([[UIScreen mainScreen] bounds].size.height==568.0){ NSString *extension = [name pathExtension]; NSString *iPhone5Name = [[name stringByDeletingPathExtension] stringByAppendingString:@"-568h"]; if (extension.length!=0) iPhone5Name = [iPhone5Name stringByAppendingPathExtension:extension]; UIImage *image = [UIImage imageNamed:iPhone5Name]; if (image) return image; } return [UIImage imageNamed:name]; } @end 

Then wherever I know, I want to download an image that also has the version of iPhone 5 that I use:

[UIImage compatibleImageNamed:@"MyImage"];

+9
source

I did it manually for all screensaver screens:

  CGRect screenRect = [[UIScreen mainScreen] bounds]; float screenWidth = screenRect.size.width; float screenHeight = screenRect.size.height; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)]; if (screenHeight==568.0) { splashView.image=[UIImage imageNamed:@" Default-568h@2x.png "];//iPhone 5 }else{ splashView.image=[UIImage imageNamed:@"Default.png"]; //other iPhones } } else { splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 20, screenWidth, screenHeight-20)]; splashView.image=[UIImage imageNamed:@"Default-Portrait.png"];// iPads } 
+3
source

EDIT : check this one as well as this one .

U how to use this line to provide a screen saver, do you have a retina or not a net display

 UIImageView *splashView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]]; 

The application detects the display of the device, respectively, the image.

If the device has a retina display, it automatically accepts Default@2x.png.

+1
source

You should change this:

 [UIImage imageNamed:@"Default.png"]; 

to

 [UIImage imageNamed:@"Default"]; 
+1
source

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


All Articles