Use different icons for iphone 3gs & iphone 4 / 4s - iphone

I have icons inside my application for uibutton & on uitable cell. Lets say I have image named "sampleImage.png". Can my app use the image named "sampleImage#2x.png iphone with retina display automatically provide it is in my app bundle ?
If yes how can write code for it because I have code like
cell.imageview.image = [UIImage named:#"sampleImage.png"];
Does it work even if I hardcoded the image name?
Any kind of help is appreciated.Thanks

Yes, UIImage will automatically use the #2x version of an image on a retina display.

Searching for an answer of your question I came across this: what is the code to detect whether ios app running in iPhone, iPhone Retina display, or iPad?
One of the answers mentions this:
There's often no need to determine directly whether you're on a retina display because UIImage handles that automatically when you use imageNamed and append "#2x" to your high resolution image file names (see Supporting High-Resolution Screens in the Drawing and Printing Guide for iOS).

Related

Do I still need low-resolution images for using Interface Builder when developing iPhone app for iOS 7?

I started to develop an iPhone app for iOS 7.
Since iOS 7 does not support devices with non-retina display for the iPhone/iPod touch, and it uses high-resolution images on non-retina iPads (iPad 2 and iPad mini) in the iPhone emulation mode, now I think we don't need to provide low-resolution images when developing an iPhone-only app which deployment target is iOS 7.
I thought it's great, but soon I faced a problem when I used a Storyboard; apparently Interface Builder can not display high-resolution images which file names end with #2x.
I feel it's really pity that we have to provide the low-resolution images ONLY for the Interface Builder...
Is there any good workaround for this? Or do we still have to provide low-resolution images if we want to use the Interface Builder?
You are correct that an iOS 7 iPhone-only app is not going to run on any single-resolution devices, so you only need to provide double-resolution images. Do what you have always done in the past: refer to your image as myImage but name the actual image file myImage#2x.png. Even better, use the asset catalog! Place the double-resolution image in the 2x slot and refer to it by the name of that image set. Either way, this will work perfectly both in the storyboard editor and in the running app; in the storyboard editor, the Media Library and things like buttons that have images will display your image's name as myImage.

Images for Retina-Display-iPhone Versus Normal iPhone

I would like to have images for my UIButtons, screen background etc in my iPhone app.
Will it work for both Retina display and normal display if I have
ButtonImage.png and ButtonImage#2x.png
Background.png and Background#2x.png
OR, should I do any extra coding so that it works for both displays?
ButtonImage.png is looking awkward on Retina Simulator. So, I should have a higher resolution image. So... please help me.
Chandu
UIImage's imageNamed: method automatically looks up the #2x images on the retina device if they're available; no additional coding is required.
If you’re putting the image in your user interface in a .xib file, or if you use the +imageNamed: class method for UIImage, then the #2x suffix is sufficient to load Retina images.
you have nothing to do, ios do the job to choose the right one
Short Answer: Yes, that is the right way to do what you want. UIImage will figure it all out for you.
However, don't forget that you can't just copy and rename a given image (say, Background.png). You need to get an image that is the same as Background.png, but four times the resolution (that's the same as double the width and double the height). Then, you can name that Background#2x.png and everything'll be peachy keen.
Here you have to use the simple image and it will work for both, Normal display as well as retina display. You just specify simple images with ButtonImage.png & Background.png in the resource bundle and retina images ButtonImage#2x.png Background#2x.png in the resource bundle.
Now you just need to specify the simple images in the code and retina image will automatically taken when running on the device with retina screen resolution i.e. iPhone 4 and later.
backgroundImageView.image = [UIImage imageNamed:#"Background.png"];
The same code will work and when iPhone 4 with retina display it will consider the image Background#2x.png from resource bundle automatically.
Let me know if you have any other question.
That's the right thing i mean whatever you are using or describing the way,but second thing
you need to do is that allocating the position for buttons.
If you will allocating position as a constant or fix by declaring x,y coordinate then it might not work,so try to allocate position of button in dynamic way so that it will take position once retina will call it.
Thanks

How to deal with different resolutions when designing images for iPhone3G and iPhone4

iPhone 4 has 960-by-640 resolution while iPhone 3G only 480-by-320. Do I have to design 2 different res. versions on of each on screen image when building app and targeting both devices? I'm building simple game with UIKit, and currently, most of my app on screen items are designed with interface builder. If separate version for each item is required than how to implement that?
No, you don't have to do double the work.
Lay out all of your screens assuming 320x480.
Create your graphics (typically PNG files) assuming a 640x960 display. Then create copies of each of the files at 50% of the original size. Use Apple's naming convention (below) and you'll be good to go -- high res graphics on retina displays and lower res graphics on non-retina displays.
Name a typical image file:
"myImage#2x.png" // for the retina image
"myImage.png" // for the non-retina image
If you load the graphic programmatically, use:
UIImage *theImage = [UIImage imageNamed:#"myImage"];
Or you can just specify "myImage" in Interface Builder - it will load the right one automatically.

Does #2x for retina work on all images, and how do i test for a retina display?

I have 3 short questions, very related:
How do i detect if the user is using a retina device?
I want to save an image inside my app in documents, if i detect that they're using a retina display phone and save the image ending in #2x.jpg will it work as it does with imported images, where it'll automatically go for that one instead on a retina device?
If i were to reference an image which didn't exist, but a #2x.jpg version existed, would it go to that by default?
Use [UIScreen mainScreen].scale, it's 2.f when it's a retina screen.
I'm not sure, but I think it'll automatically detect #2x-versions here too (as long as you use UIImage to load them).
If it's a retina screen, it'll look to the #2x-version first, if it doesn't exist, it'll fallback to the default-version. If that version doesn't exist, it'll return nil. A non retina screen however, will never look to the #2x-version.
You call it a x2.jpg-file, however, it isn't:
Your default file is something like background.png.
Then your retina file should be like background#2x.png.
You load the image using UIImage *image = [UIImage imageNamed:#"background.png"];.
In most situations you don't need to know if it's a retina screen, iOS will handle this.

Retina compatibility question: Can I add #2x to my own images for retina compatibility?

If I append the #2x suffix to my own images, will iOS 4 automatically replace my images with the Retina compatible ones, or does that only apply to Apple defined images? (Icons, for example.)
Yes, of course. If you display a myimage.png in your app, you may add a retina ready myimage#2x.png, so it is shown on retina devices.
See this guide for further information.
From the documentation:
The bundle- and image-loading routines
automatically look for image files
with the #2x string when the
underlying device has a
high-resolution screen.
So using for example [UIImage imageNamed:#"MyImage.png"] will automatically look for MyImage#2x.png when running on a device with retina display.