What is the new naming convention for images for the 4-inch retina display?
For an image named background.png you add #2x to the name (background#2x.png) to tell iOS to use that one for devices with the retina display.
What would the suffix be for iPhone 5's screen size?
You can use my #defines to help you with these images:
#define isPhone568 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)
#define iPhone568ImageNamed(image) (isPhone568 ? [NSString stringWithFormat:#"%#-568h.%#", [image stringByDeletingPathExtension], [image pathExtension]] : image)
#define iPhone568Image(image) ([UIImage imageNamed:iPhone568ImageNamed(image)])
Just give your images the -568h#2x.png notation, and use iPhone568ImageNamed to get a name for standard name or iPhone 5/new iPod.
Usage example from the comments:
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:iPhone568ImageNamed(#"mainscreen.png")]];
Related
So for some reason, whenever I run an ipad simulator in xcode, it does not recognize that it is running as an ipad and pulls the background image for the iphone on the ipad. This makes it look really stretched and distorted.
Here is part of my code from my ISUtilsPack.h:
//device type definition
#ifdef UI_USER_INTERFACE_IDIOM
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#else
#define IS_IPAD false
#define IS_IPHONE true
#endif
And here is my code from my UIManager.m:
//background image
+ (UIImage *)mainBackgorundImage
{
UIImage *image = [UIImage imageNamed:#"iphone-568"];
if(IS_IPAD)
image = [UIImage imageNamed:#"ipad"];
return image;
}
Every time xcode is pulling the iphone-568 image for the background and not the ipad image even when running the simulator for ipad. Does anybody know why this is?
Thanks!
So I need to differentiate the screen size (3.4 or 4 inch) devices in order to select the right image to display.
Here is what I did: -- from here
#define IS_PHONEPOD5() ([UIScreen mainScreen].bounds.size.height == 568.0f && [UIScreen mainScreen].scale == 2.f && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
if(IS_PHONEPOD5()) {
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay-568h#2x.png"];
} else {
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay.png"];
}
Regardless of the screen size the image view always contains the larger image and extends off the screen (on the 3.5")
Am I doing something wrong, I have tried a few other things and it has always done the same thing.
EDIT: Even if I don't ever select the larger image it still is the one on screen at runtime:
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay.png"];
It is still the larger image?
Have you checked UIViewContentMode of UIImageView? Try it by setting UIViewContentModeScaleAspectFit and check if it works.
don't use #2x in your image string. because every retina device will automatically take care of that.
use should save your image text like image#2x.png but i must use only without #2x extention. use properly like [UIImage imageNamed:#"image.png"]; this is for 3.5 inch.
How to use 4 inch screen use extention with -568h. Eg:
image-568h#2x.png. use like [UIImage imageNamed:#"image-568h.png"];
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay-568h.png"];
more detail
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/App-RelatedResources/App-RelatedResources.html
Edited:
self.navigationcontroller.navigationbar.translucent = NO;
In a universal app should I have a image.png, image#2x.png, image~ipad.png and image#2x~ipad.png for every image in my project?
Or should I just use my wider iPad images and have iOS scale them down to iPhone for me?
There's a lot of images so I'm a bit concerned about the file size...
Thanks!
Not necessarily but you can see code below. If you are targeting low res iPads and Low res iPhones you would have one set of icons for each. So if you are targeting iPad retina and iPhone retina that is on set of icons too so if you have an image called car.png and car#2x.png you would have 2 icons that cover all 4 models mentioned.
You can of course have images specific for iPad as the screen is larger then in your logic you would show either or depending on the device idiom..like below
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//Device is iPhone
//This would give you low res icon for iPhone and if device is retina system will show it
UIImage *car = [UIImage imageNamed: #"Car.png"];
UIImageView *carImage = [[UIImageView alloc]initWithImage:car];
}
else {
//Device is iPad
//This would give you low res icon for iPad and if device is retina system will show it
UIImage *carLarge = [UIImage imageNamed: #"CarLarge.png"];
UIImageView *carImage = [[UIImageView alloc]initWithImage:carLarge];
}
I would like to better understand the iphone resolutions etc.
I have an application that has a basic buttonView and logoView. I have output the height of the logoView which will auto fit in height depending on screen size.
For the iphone5 I have 318 to work with.
For the iphone4(<) I have 230 to work with.
My question is, how should I handle the image used for the background of this view. Would I create one three separate images for the following?
-iphone3 etc (230)
-iphone4 retina (230 size, #2)
-iphone5 retina (328 size, #2)
Or would I create only the 2x 230 images, and can I stretch the image to 318 when an iphone5 is used and more space is available?
It all depends on your image:
If your Image can be stretched, UIImageView will do all the work.
If only a part of you image should be stretched you should use this:
imageView.image = [imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)];
If your image can't be stretched you should then do different images for the phones and change them in runtime.
UPDATE
For the last point you could do something like this in your viewDidLoad method:
BOOL isIPhone = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone;
BOOL isIPhone5 = isIPhone && ([[UIScreen mainScreen] bounds].size.height > 480.0);
if (isIPhone5) {
imageView.image = [UIImage imageNamed:#"iphone4image.png"];
} else {
imageView.image = [UIImage imageNamed:#"iphone5image.png"];
}
iOS 8 has different size classes for different screens. It's explained very well here. Every iOS Developer should go through this link:
If you want to make this a little more succinct you can account for longer screen sizes with a macro.
#define ASSET_BY_SCREEN_HEIGHT(regular, longScreen) (([[UIScreen mainScreen] bounds].size.height <= 480.0) ? regular : longScreen)
Example usage:
- (void)viewDidLoad
{
[super viewDidLoad];
self.splashScreenImageView.image = [UIImage imageNamed:ASSET_BY_SCREEN_HEIGHT(#"Default", #"Default-568h")];
}
I have in my viewController.m written the background code:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"image.png"]];
And I have the correct names of the different pictures:
image.png for non-retina display (320x480)
image#2x.png for retina display (640x960)
image-568h#2x.png for iPhone 5 (640x1136)
But when I run it in the simulator it does not take the image-568h#2x.png for iPhone 5 screen it only takes the image#2x for 4s screen and scale it to fit the screen... I dont know if there is any coding to use the image-568h#2x for iPhone 5 screen?
Im using Xcode 4.5
iPhone 5 is retina, just like iPhone 4 and 4S, and the #2x-image will be used automatically for all these devices. It's only the startup-image that is called "-568h#2x" for iPhone 5. You need to write some code to use a different image, something like this would work:
NSString *filename = #"image.png";
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (screenRect.size.height == 568.0f)
filename = [filename stringByReplacingOccurrencesOfString:#".png" withString:#"-568h.png"];
imageView.image = [UIImage imageNamed:filename];
if you are trying to use [UIImage imageNamed:#"image.png"] and expect image-568h#2x.png to be picked automatically from the bundle for iPhone 5, it will not work.
Automatic picking works only for iPhone 4 and 4S.
Only the Default image named as Default-568h#2x.png will be picked automatically in iPhone 5.
for normal images, if you have separate image for iPhone 5, try using this code
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen
} else {
// code for 3.5-inch screen
}
I believe it is incorrect to assume that you can apply the -568h#2x trick to all image files. I think it only works for Default-568h#2x.png. This is the file that iOS looks for at app launch on a 4" display device, as well as the "flag" to enable 4" display support in the SDK. For example, once you include this specific file, your table views will fill the screen.
I have not read anything to suggest that you can simply provide any image with the -568h#2x file name component and have it be used automagically. You'll have to do that yourself based on the screen size, e.g. [UIScreen mainScreen].bounds.size.height.