image sizes for different iOS devices - ios

I'm new to iOS programming. I want to ask these questions about image sizes for different screen sizes
Do x, 2x and 3x image sizes suffice for all the iOS devices? I mean if I have an image named "background.png", will background.png, background2x.png and background3x.png be sufficient for all the iOS devices/screen sizes?
If not, do different iPad models require some other image sizes(other than x, 2x and 3x)? ....... A link for explaining image sizes for different screen sizes/devices will be appreciated. Thanks

yes x, 2x and 3x image sizes is necessary to develop application for all iOS devices , but the size of images can be different for iPad & iPhone devices, depending on your application UI for iPad & iPhone .
For better understanding for background images please have a look Adaptivity and Layout
One of the best article related to your query - Adaptive Layout Tutorial in iOS 9: Getting Started

If you made images for #1x, #2x, and #3x, name them like this:
iPhone 3: myImage.png
iPhone 4, 4S: myImage#2x.png
iPhone 5, 5S: myImage-568h#2x.png
iPhone 6, 6S: myImage-667h#2x.png
iPhone 6P, 6PS : myImage-736h#3x.png
Then you can just call the blow method like:
UIImage *myImage = [self deviceSizedImageWithName:#"myImage.png"];
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenSize [UIScreen mainScreen].bounds.size
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4 (IS_IPHONE && kScreenHeight == 480.0f)
#define IS_IPHONE_5 (IS_IPHONE && kScreenHeight == 568.0f)
#define IS_IPHONE_6 (IS_IPHONE && kScreenHeight == 667.0f)
#define IS_IPHONE_6P (IS_IPHONE && kScreenHeight == 736.0f)
- (UIImage *)deviceSizedImageWithName:(NSString *)imageNamed
{
NSString *imgExtension = [imageNamed pathExtension];
NSString *imgName = [imageNamed stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#".%#", imgExtension] withString:#""];
BOOL removedExt = [imgExtension length];
UIImage *image = [UIImage imageNamed:imageNamed];
if (IS_IPHONE_5) {
if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-568h.%#", imgName, imgExtension]];
else image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-568h", imageNamed]];
if (!image) return [UIImage imageNamed:imageNamed];
} else if (IS_IPHONE_6) {
if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-667h.%#", imgName, imgExtension]];
else image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-667h", imageNamed]];
if (!image) return [UIImage imageNamed:imageNamed];
} else if (IS_IPHONE_6P) {
if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-736.%#", imgName, imgExtension]];
else image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-736", imageNamed]];
if (!image) return [UIImage imageNamed:imageNamed];
}
return image;
}

Related

Detect iPhone5, 5s, 6, and 6 plus

Can someone show me how to update the below code to work with iPhone 5, 5s, 6, and 6 +? When I test the simulator for the aforementioned devices, no image is set as the background...
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:_Image];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
//I EXCPECTED THIS NEXT LINE OF CODE TO WORK TO SET A SEPARATE BACKGROUND FOR IPAD...
UIImage *Image = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 480) {
// iPhone, iPod Touch
Image = [UIImage imageNamed:#"image.png"];
}
if ([[UIScreen mainScreen] bounds].size.height == 568) {
// supposably iPhone 5 but only works for 4
Image = [UIImage imageNamed:#"image.png"];
}
if ([[UIScreen mainScreen] bounds].size.height == 1024) {
// iPad
Image = [UIImage imageNamed:#"image.png"];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:Image];
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
Then compare in your if statement.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:_Image];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
UIImage *Image = [[UIImage alloc]init];
if(IS_IPHONE_5)
{
Image = [UIImage imageNamed:#"image.png"];
}
if(IS_IPHONE_6)
{
Image = [UIImage imageNamed:#"image.png"];
}
if(IS_IPHONE_6P)
{
Image = [UIImage imageNamed:#"image.png"];
}
I don't see why you're doing it this way though.... Is it because your image is becoming distorted based on different devices? Because there are easier and more efficient ways to solve this in IB.

Adjust iPhone 5 and iPhone 3.5' Screen Size Without Using Autolayout

Hi I am developing an IOS application. I don't use autolayout. If my app run at iPhone 5(4") simulator then showing black top and bottom place. Simulated metricks size also none. What can I do.
Thanx
Just add an splash screen image named Default-568h#2x.png to your project folder black space will be removed automatically.
Use UIView autoresizingMask property.
You can also check screen size and load specific image for iPhone 5.
I use some like this -
#implementation UIImage (iPhone5)
BOOL iPhone5Screen();
+(UIImage*)imageNamed5:(NSString *)name
{
UIImage *retImage = nil;
if (iPhone5Screen()) {
NSString *imageName = [name stringByDeletingPathExtension];
if ([imageName hasPrefix:#"#2x"]){
imageName = [imageName substringToIndex:imageName.length - 3];
}
NSString *iPhone5ImageName = [NSString stringWithFormat:#"%#-568h", imageName];
retImage = [UIImage imageNamed:iPhone5ImageName];
if (retImage) {
return retImage;
}
}
return [UIImage imageNamed:name];
}
BOOL iPhone5Screen()
{
BOOL bRet = NO;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
bRet = NO;
}
else if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 568){
bRet = YES;
}
}
return bRet;
}
#end

IOS UIImage of launch image for current device

is there way to get launch image as UIImage for current device?
or UIImageView with an image
You just need to get Default.png which will have any #2x applied as necessary.
- (UIImage *)splashImage {
return [UIImage imageNamed:#"Default.png"];
}
If you care about getting the iPhone 5 specific one you need to do a height check:
- (UIImage *)splashImage {
if ([[UIScreen mainScreen] bounds].size.height == 568.0){
return [UIImage imageNamed:#"Default-568h.png"];
} else {
return [UIImage imageNamed:name];
}
}
First reduce the name of the launch image using [UIDevice currentDevice] and [UIScreen mainScreen] and then read the image like you would for any other resource image.
[UIImage imageNamed:yourLaunchedImageName];
You can write something like this.
Here we are figuring out what splash image to show (depending on device and scree rotation) and adding it as a subview to our window.
- (void)showSplashImage
{
NSString *imageSuffix = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
imageSuffix = [[UIScreen mainScreen] bounds].size.height >= 568.0f ? #"-568h#2x" : #"#2x";
}
else
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
imageSuffix = UIInterfaceOrientationIsPortrait(orientation) ? #"Portrait" : #"-Landscape";
imageSuffix = [UIScreen mainScreen].scale == 2.0 ? [imageSuffix stringByAppendingString:#"#2x~ipad"] : [imageSuffix stringByAppendingString:#"~ipad"];
}
NSString *launchImageName = [NSString stringWithFormat:#"Default%#.png",imageSuffix];
NSMutableString *path = [[NSMutableString alloc]init];
[path setString:[[NSBundle mainBundle] resourcePath]];
[path setString:[path stringByAppendingPathComponent:launchImageName]];
UIImage * splashImage = [[UIImage alloc] initWithContentsOfFile:path];
UIImageView *imageView = [[UIImageView alloc] initWithImage:splashImage];
imageView.tag = 2;
[self.rootViewController.view addSubview:imageView];
}

can't display 640x1136 image on iphone5

I've added a Default-568h#2x.png launch image to my app. The app needs to display a second launch image after the "real" launch image. Because the UIImage imageNamed: method doesn't automatically load the taller image the way it automatically loads retina images, I've added some code to detect the screen size and display the correct image:
-(void)pickRightImage
{
CGSize result = [[UIScreen mainScreen] bounds].size;
UIImageView *imgv = [self loadingImage];
UIImage *img;
if(result.height == 480)
{
img = [UIImage imageNamed:#"loading_screen.png"];
} else if([UIScreen mainScreen].scale == 2.f && result.height == 568) {
// iPhone 5
img = [UIImage imageNamed:#"loading_screen-568h#2x.png"];
}
[imgv setImage:img];
}
The imageView takes up the whole screen in the NIB, which is named MainWindow, and I have selected the checkbox named "Full Screen At Launch" However, the image never takes up the whole screen. (Although the launch image does.) The second image is letter boxed just as if it were a smaller image, and I had never included the tall launch image.
Is there anyway to programmatically display a full screen image on the 4 inch iphone5? Why is my image always resized?
[UIImage imageNamed:] will take care of adding the #2x for you. So you should just specify
img = [UIImage imageNamed:#"loading_screen-568h.png"];
It's also useless to test both a 4" screen AND the Retina criteria (scale = 2). All devices that have a 4" screen (568px tall) are Retina displays, so you can assume that if height == 568, the user has an iPhone 5 : replace
if ([UIScreen mainScreen].scale == 2.f && result.height == 568)
with
if ([UIScreen mainScreen].bounds.size.height == 568)
and you're good.
I tested this on the main view controller. Also, on Target > Summary > Status Bar > Visibility check "Hide during application launch".
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *iv = [[UIImageView alloc] initWithFrame:self.view.bounds];
iv.image = [UIImage imageNamed:#"Second-default568h#2x.png"];
[self.view addSubview:iv];
}

how to load appropriate Default.png to UImageView?

I have Default splash screens with the names:
Default-568h#2x.png, Default-Portrait.png, Default.png, Default#2x.png and so on for all types of devices.
I know that the system automatically selects the appropriate splash screen for the specific device and displays it.
The questions: is it possible to know which image the system selected? How to load the appropriate image selected by the system to the UIimageView.
I tried this:
UIImageView *splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
splashView.image=[UIImage imageNamed:#"Default.png"];
But it loads only the image with name Default.png for all types of devices(iPhone 4, 5, iPad).
Do i need to manage it manually? I mean to load the appropriate image after identifying the device type?
I found this question after running into the same problem. Seems that if you use [UIImage imagedNamed:#"Default"]; iOS will detect retina versus non-retina and apply the #2x but it won't detect an iPhone 5 and apply the -568h
The solution I came up with was to write a category on UIImage that checks the main window's height and returns the appropriate 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 load an image that also has an iPhone 5 version I use:
[UIImage compatibleImageNamed:#"MyImage"];
I did it manually for all splash 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
}
EDIT: check this and also this out.
U how use this line to provide splash screen whether u have retina or non-retina display
UIImageView *splashView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default.png"]];
Application detects device display takes image accordingly.
If device has retina display then it takes Default#2x.png automatically.
You should change this:
[UIImage imageNamed:#"Default.png"];
to
[UIImage imageNamed:#"Default"];

Resources