When supporting retina, and non-retina displays you would provide the image.png & image#X2.png with relating resolutions.
I understand that iOS will decide which image to use based on the user's device. However, I'm using the images via code, not storyboard.
self.imagePortrait = [UIImage imageNamed:#"image.png"];
Even though I have not specified image#X2.png, will iOS make use of it?
In this case yes. UIImage's -imageNamed: is smart enough to choose the correct resource. (And note that the suffix is #2x, not #X2.)
From the docs:
If the screen has a scale of 2.0, this method first searches for an
image file with the same filename with an #2x suffix appended to it.
Other methods (like -initWithContentsOfFile:) do not have these smarts built in.
Related
I recently asked a question about why we use the 3 different image types listed above, and got good/detailed responses. It also lead me to question something else...
Assume I am working in XCode (with SWIFT) and have the three image sizes #1x, #2x, #3x for a particular say UIImage on an iPhone. Now, I want it to look nice on iPad, so I add some scaling. However, this will change the sizes of the 1x, 2x, 3x images I need for the iPad, so I need to make 3 more to compensate for that new size. Now how will XCode differentiate between the two? Do I need to check what size screen I am using and then choose the images?
I read that assets could help me, but that only deals with iPhone vs. iPad, what if I scale my images across iPhone 4, 5, 6 and iPad. That's a lot of different sizes and #x images, and I read Apple doesn't like device checking so I'm not sure how that would happen.
I hope this makes sense. I get the #1x, 2x, 3x per device, but if my app is being designed for universal device use, then how do I manage all of these different sizes in code for the proper device?
You really should use image asset catalog. It makes it almost too easy. Here is the definition Apple gives so you know it's not hearsay:
Image sets: Used for most types of images, an image set contains all the versions, or representations, of an image that are necessary to support various devices and scale factors.
And it goes on further:
Each set in an asset catalog has a name. You can use that name to programmatically load any individual image contained in the set. To load an image, call the platform specific class method, passing in the name of the set that contains the image. The OS will load the image from the set that is most appropriate for the current scale factor
So there really is not much for you to do. I don't know where you heard the opposite
See Documentation here
You need to check the size of the biggest image, if you are using an iPad figure out the size of the UIImageView frame. For instance if it is 400*400pts then your image will be 1200*1200. Just use a single image. I don't think you need to include the #2x and #1x. The 1200*1200 covers your bases for the largest possible image and scaling down will still have plenty of pixels left over for the full #3x resolution on other devices and so on. This is how I understand it anyway. I also wondered this myself and only found #1x #2x #3x useful for things that I didn't scale up as the screen size increased such as some buttons.
In my app I am using images of size 155*155pts so I am supplying it with an image of 310*310px resolution. I know that I can use image.png image#2x.png and image#3x.png and then [UIImage imageNamed: image] to select the image appropriate for the resolution. My question is do I really need to include a lower resolution version of size 155*155px, won't the UIImageView it's displayed in just scale it appropriately? A similar question for the iphone6+, if I don't include the #3x version will it just use the #2x and display it as clearly as a standard retina screen would?
Even though there are plenty of answers at SO discussing this topic
(Google them) e.g. How to handle image scale on all the available iPhone resolutions?
Its just a recommendation to to use scaled up images with #2x and #3x in your app. You don't have to create them. From my experience in making apps, in almost all of them I have never used multiple images. I create one UIImage and use it for all the phone sizes. I then either use auto layout or manually adjust the width height of UIImages myself.
There is a reason I do that is lets say one of your sample1.png image is 1MB then you will need to create 3 of them.
sample1.png
sample1#2x.png
sample1#3x.png
You just doubled or tripled your binary size which is bad for downloads. There always be users running non-retina devices and it would be shame to not support them. Can you only make retina enabled apps, of course you can and Apple will approve those as they will be testing your apps on the latest devices but the best approach is to support all devices.
It depends, if you only support non-retina devices you donĀ“t have to add the normal image size anymore.
The scaling for the 6+ works with the #2x images but i guess you will see a quality difference
I just want to use one set of image asserts for all iPhones and iPads(Universal), Is it possible to do the same? or what will be the best solution for the making a build lighter.
YES, it is possible ti use one image asserts for all devices. just add new image set
in assert by Right click. And the add images for required devices in created Image set.
you can use Image set but it's name,and images are automatically loaded with different devices.
UPDATED
if you want to titlelogo.png for all device then make new image set in Images.xcassets and name it as AssetTitlelogo. and the add all images in it as in screenshot.
You use image by
[UIImage imageNamed:#"AssetTitlelogo"]; /// use your asset Name
It's load right image for right Device..
Yes, if you use a retina image without the #2x or #3x in the name and your UIImageView is set to ScaleToFill it will automatically scale down your image on non-retina devices. it is easy to test with the simulators to see how the image will look at the different screen resolutions.
the trade off is non retina devices will suffer with more memory usage than is needed for them (and usually the non-retina devices are some what limited in memory already compared to the latest apple products) but if you arent dealing with a lot of images on the screen at once then this shouldnt have an adverse effect
I am making images for my UITabBar. I am making them of size 60x60, because that's what retina screens use. However, when I use that size, it shows up too big in the bar, so you can only see part of the image. When I reduce it down to 30x30, it works, but that size is supposed to be for non-retina displays. Why does it not show up properly when I use 60x60?
You probably may have to rename your retina image to imagename#2x.png.
You were getting this behavior because you were supplying a high-resolution image, when iOS was looking for a standard resolution image.
iOS automatically selects the appropriate image size for you, depending on the resolution of the accessing device. So you will be responsible for supplying a "standard resolution" image, for non-retina devices, and a "high resolution" image, for retina displays. The way you do this in iOS is to append "#2x" to the end of your filename, but before the file extension, like this:
my-image.png // for non-retina displays (Ex: 30x30 dpi)
my-image#2x.png // for retina displays (Ex: 60x60 dpi)
my-image#3x.png // for retina displays(plus editions) (Ex: 90x90 dpi)
Then, when you are referencing files in your XCode project, you only need to supply the filename to the standard resolution (e.g, "my-image.png") and if the accessing device has a retina display, then XCode will automatically select the file with the "#2x" suffix for you. This is very convenient, because it saves us developers from having to detect whether or not the device has a retina display, and which image we need to supply.
Here is a code example:
// Select an image named "my-image.png"
UIImage *img = [UIImage imageNamed:#"my-image.png"];
// If the device this code is run on is a retina device,
// then Xcode will automatically search for "my-image#2x.png" and "my-image#3x.png"
// otherwise, it will use "my-image.png"
You can read more on the subject via the Apple Developer's Site: Optimizing for High Resolution
We have a mystery !
In our app we use only retina images (#2x marked).
Until yesterday non-retina devices showed the images well even when we called the images without #2x at the end.
For example if the file name is 'fun#2x.png' we called [UIImage imageWithName:#"fun"].
Today it stopped working and now we have to call [UIImage imageWithName:#"fun#2x"] for the image to display. (The device is iPhone 3GS iOS 5.1).
We are now afraid that we don't understand something about the retina naming.
What changed ?
What is the correct way to deal with it?
Thanks
Shani
You should provide non-retina images! The downscaling isn't a good option. The problem here is that iOS tries to find the fun.png image and doesn't find it. Therefore can't present something.
I have no explanation why it worked before. Are you sure?
In apps I have worked on if I am only supplying 1 image (the 2x one) than I just use the full image name, ie:
[UIImage imageWithName:#"my-image.png"]
When I am supplying images for certain buttons that I need have 2 versions of, I use:
[UIImage imageWithName:#"my-image"]
Doing it this second way, you must supply a retina & non-retina image with that base name
Like dasdom said it should have not been working before that way-- and even if it somehow was you should stick to what I said above if you want your images to always display.