UIImage screen size condition weird behaviour - ios

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;

Related

What is the code when I want to show a #2x image in app

Today when I search some information about #1x #2x #3x
something confuses me
are these only used as app icon? Because most messages always give me the example of the icon.
and If it can be used in app view, how is the code?
like this?
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: #"background.png"]];
Then, iOS will change #1x and #2x itself? OR I need to code like this:
#define deviceIsIPhone5 ([UIScreen mainScreen].applicationFrame.size.height == 568 ? YES : NO)
if (deviceIsIPhone5)
{
self.backgroundImage.image = [UIImage imageNamed:#"background#2x.png"];
}
else
{
self.backgroundImage.image = [UIImage imageNamed:#"background.png"];
}
You don't have to think about #2x #3x.iOS automatically handles it.
All you need to care about is to put right size image into the right box(.xcassets) if you want to show image properly in all iOS devices.
no need to care about 2x,3x and png.
just enter image name only. ios will automatically check image to use according to screen dimensions.
self.backgroundImage.image = [UIImage imageNamed:#"background"];

Background image to fit 3.5" and 4" screens

I have been looking online for a while now on how to make my images fit the iPhones 3.5" screen and 4" screen but i cant find anything.
I have a -568#2x.png image, a image.png, and a image#2x.png but when i run the simulator for the
iPhone 3.5" the image is stretched to far to the right.
I need help to fix this, please.
In terms of a Background image. Just add a UIImageView and which displays an image depending on screen size. (You will need to make two sets of images for this so you don't get any stretching).
You can then set which image to be displayed in the UIImageView by adding a simple if statement to check which screen size you have in the viewDidLoad method like this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height == 480) {
// 3.5 inch display
[background_image setImage:[UIImage imageNamed:#"small_background.png"]];
}
else if (result.height == 568) {
// 4 inch display
[background_image setImage:[UIImage imageNamed:#"big_background.png"]];
}
}
Don't forget to declare "background_image" as a UIImageView in your header file:
IBOutlet UIImageView *background_image;

iOS 6.1 XCode 4.6 - imageNamed never return #2x version

Both files "flipImage.png" and "flipImage#2x.png" are in project. In -[FlipsideViewController viewDidLoad] I have the following code. The sanity check (thanks to other stackoverflowers) correctly reports retina or no. But in either case, the image loaded is the small one, verified by its height. Why no auto-load of appropriate image? OK, I can see workarounds, but I'd like to Use The System when possible.
UIImage* flipimage = [UIImage imageNamed:#"flipImage.png"];
NSLog(#"Image height = %f", flipimage.size.height); // always 416, never 832 :(
// Sanity check.
if ([[UIScreen mainScreen] respondsToSelector:#selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0)) {
NSLog(#"Retina");
} else {
NSLog(#"Non-retina");
}
iOS retina displays don't work that way. The height of an #2x image and a standard resolution image will be the same on the device and in your code. When you create an image on the screen that is 416 points x 416 points, it doesn't change height just because it's on a retina display versus a non-retina display.
The difference is that #2x images have higher resolutions so they show more pixels per point which is what the retina displays are using instead of pixels.
So essentially, all you need to do is use the standard resolution filename for any image you use within the app and the OS will automatically take care of replacing it with the higher resolution images if it's on a retina display.
According to the previous comments... and without having to change your code a lot, why don't you just do this:
UIImage* flipimage = [UIImage imageNamed:#"flipImage.png"];
NSLog(#"Image height = %f", flipimage.size.height * [UIScreen mainScreen].scale);
That should returned you the size (Number of points * number of pixels per point).

Handling different iphone screen sizes/resolution for background images

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")];
}

Subclass UIImage imageNamed:?

I am on my voyage to supporting the iPhone 5. I am well aware that there is iOS 6's auto layout feature in Interface Builder. This is great however, sometimes I want to load my own images if its an iPhone 5.
So some of the images in my game for iPhones are exactly the screen's size either (320x480) or (640 x 960). So if I am on an iPhone 5 those images will not cover the entire screen. So I am going to make (640 x 1136) images for those full screen images.
Now I know that if I simply name the image: MyImage-568h#2x.png it won't load. So, how would I subclass UIImage so that it will load the -568h#2x image thats not the default image?
Thanks!
The lazy way:
UIImage * img = [UIScreen mainScreen].bounds.size.height > 500 ? [UIImage imageNamed:#"Default-568h.png"] : [UIImage imageNamed:#"Default.png"];
I've done this in an app where the main screen on devices without a camera is Default.png; it doesn't seem too hacky because we would need to change Default.png to support new devices anyway.
The less lazy way: Condition on the space you need to fill, not the screen size.
The solution you asked for isn't really generic enough to warrant a class, but here it is anyway:
#implementation MyImage
+(UIImage)imageNamed:(NSString*)name {
if ([UIScreen mainScreen].bounds.size.height > 500) {
UIImage * img = [UIImage imageNamed:[name stringByAppendingString:#"-iPhone5"];
if (img) { return img; }
}
return [UIImage imageNamed:name];
}
#end
There's really no need to subclass UIImage. If you're feeling really hacky, you can patch/swizzle/otherwise +[UIImage imageNamed:] to point to a custom method, but I really don't recommend that except for debugging.

Resources