I'm creating a universal iOS application (supports iPad and iPhone 4 and later). In this application, I have an image that spans the width of the screen on every device (and maintains a constant aspect ratio). In order to be as memory-efficient as possible, I created a version of this image for each device I support, which each version of the image sized to exactly fit on the device it's intended for.
What is the best way to store these images in my Xcode project, and load the correct image in my code?
Right now I just have 2x and 3x image assets for the image (and a 2x for iPad) and load it with the following line of code:
let imageView = UIImageView(image: UIImage(named: "image"))
But that doesn't allow me to have a different image for, say, iPhone 6 and iPhone 5 because they both load the 2x image.
Related
I am trying to put a background image on a UIViewController.However i cannot figure out the size needed because the image should support devices from iphone 4s to iPAD...Here are few questions:
1) Should i put those large image on image xcassets on 1x,2x,3x size,if so what should be the size of those images?
2) Or Should i Copy a high resolution image on the bundle itself again if so what should be the size?
We have differnet-2 sizes in assets like here:
(Only for iPhones)
What I found is that, you should opt for first option and 750X1333, 1080X1920 and 1242X2208 should be the 1x,2x and 3x.
You can add more by clicking on tick sign and give sizes like above we give for the iPhones.
Designer will give you proper sizes with respect to resolution to each ipad and iphone
Well it depends on what kind of image you want to display
If it's a solid or pattern image that won't have any different if scaling up / down then I suggest that you put it in image xcassets and use for all supported devices. You should put the largest size image, you can find all screensizes from this: http://iosres.com/
Otherwise, if it's a picture with details like a portrait or food dish, etc. You have to drop it manually with some photo editors like photoshop then add it to xcassets separately for iPhone and iPad
In place of 1x,2x & 3x images take single PDF image and place in image assets it ll fit for all comparability devices.
I think this code will help:
var backGroundImageView :UIImageView! //declare
///set image contentMode in viewDidLoad()
self.backGroundImageView.contentMode = .scaleToFill
However, you should check whether the image is in landscape or portrait orientation.
Image xcassets on #1x,#2x,#3x size will match iPhone 4S screen size automatically, #2x is for the iPhone 5, #3x for the Plus.
As for the iPad you can check the Apple Developers documents.
I'm a bit confused here. If, say, I want to set an image as background (full screen) for my ios app (iphone), what are the sizes for the image I have to make? I googled and found no clear answer yet.
Using image assets gives no explanation either. I tried creating new image set, it just shows 3 slots for 1x, 2x, and 3x. How the xcode differentiate between what image to use for iphone 5 and iphone 6 (different ratio)?
EDIT
I seem to be unclear about my question. If the image for iphone 5 and 6 is the same, doesn't the aspect ratio differ? It will be stretched on one device or vice versa on the other and I don't want that. Back before the iphone 6, I just create image .png, #2x.png, and -568h#2x.png with the size 320x480, 640x960, and 640×1136. So my question is, for iphone 6 and 6+, what is the naming and size for my images?
If, say, I want to set an image as background (full screen) for my ios app (iphone), what are the sizes for the image I have to make?
You can use [UIScreen mainScreen].bounds's width & height to setup your view frame.
it just shows 3 slots for 1x, 2x, and 3x
They're not used for handling ratio of displaying, just for display solution (retina or not). You can just use xxx.png in your code, and it'll choose to use xxx#2x.png or xxx#3x.png appropriately.
How the xcode differentiate between what image to use for iphone 5 and iphone 6 (different ratio)?
UIImageView has an instance method called -setContentMode:, you can use UIViewContentModeScaleAspectFill mode for displaying in different devices w/ different ratio, i.e., some parts will be cut off.
This link should give you an overview of the differences between the 1x, 2x and the 3x image assets which we get once we create a new image asset.
http://9to5mac.com/2014/08/29/support-for-3x-image-assets-found-in-latest-ios-8-beta-ahead-of-larger-display-iphone-6/
Also when you create a new image asset, you can select image for specific devices such as (iPhone and iPad), if you are only developing for certain devices.
Select the devices as "Device Specific" from the Image Set Shown on the right side of the image.
As for you question, iPhone 5 and iPhone 6, xcode will take the same image assets i.e. 2x, for iPhone 6 Plus it takes the 3x image.
App Icon and Launch Image
Now for the App icon and the Launch image, this following link will guide you through.
http://www.iphonelife.com/blog/31369/unleash-your-inner-app-developer-managing-images-xcode-5-asset-catalogs
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
for the launch image there are different options available, such as you can individually set images for the landscape and portrait images for different devices.
I have this iPhone app targeted to iOS 7.
I have created an entry on .xcassets called mainImage that is device specific for iPhones. So xcode presents me with 4 slots I have to fill with images. that has these images: 1x, 2x, Retina 4 2x and 3x.
I have filled each slot with the following images in order:
1x = 320 x 480 pixels image
2x = 750 x 1334 pixels image
Retina 4 2x = 640 x 960 pixels image
3x = 1242 x 2208 pixels image
Then I have a viewController on storyboard that is adjusted with the iPhone 6 size and the imageView there is adjusted to load just "mainImage".
The image shown there is the iPhone 4 2x stretched, not the correct one. It should be showing the 750x1334 image.
If the imageView is adjusted with "mainImage", wasn't it supposed to load the correct image for the viewController size?
I run that on iPhone 6 and indeed the image shown is the iPhone 4 image stretched.
Anyway to fix that?
It does not work like that. You can have separate images for different screen densities, not for different screen sizes. You have to add a separate entry for each screen size and select the image yourself. Launch image is the only asset that works automatically with different screen sizes.
How to specify size for iPhone 6/7 customised edge-to-edge image?
Follow this guidelines for each iDevice:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html#//apple_ref/doc/uid/TP40006556-CH27-SW1
In fact there is one more limitation/bug on xcassets, beyond the impossibility to localize it to different languages, that is the ability to load a different image or better the correct to iPhone 6. Apple did not realized that the way xcassets work now will not load the correct image if you care using full screen images.
The solution for that is not to use xcassets, create the images you need and a class to call the correct image based on the screen size/needs.
I wanted to know that how we will manage images assets for different sized iphones in xcode,suppose i have an image name abc.png for iphone 5
abc.png non 20X20
abc#2x.png 40X40
as i can set image name abc.png in xib but as size of iphone 6 and iphone 6+ is different so how i can use abc.png for those large size iphone
I have images for all resolutions
abc.png (for iphone 5)
abc6.png (for iphone 6)
abc6plus.png (for iphone 6+)
i have below doubts in my mind
1. i have to mannualy set image in code for iphone6 and iphone 6+ by checking the screen size?if yes then please let me know do i have to use only one image that is 1X or have to add 2X image also for all device
2.i think that as these iphone5,iphone 6 and iphone6Plus are retina displays so i should use only 2X images for these displays?
3.Do we have to use images like giving then name of image in xib abc.png and placing the abc#2x.png for iphone 6 and abc#3x.png for iphone6+ in bundle only and xcode will pick corresponding image at runtime?
Please suggest
You can use UIImageAsset
A UIImageAsset object is a container for a collection of images that represent multiple ways of describing a single piece of artwork. A common use case for UIImageAsset is the grouping of multiple images of the same item at different display scales.
As you said
3.Do we have to use images like giving then name of image in xib abc.png and placing the abc#2x.png for iphone 6 and abc#3x.png for
iphone6+ in bundle only and xcode will pick corresponding image at
runtime?
you just need to add different resolutions of same image in assets(Images.xcassets) ( i.e abc.png , abc#2x.png and abc#3x.png ) the rest of work will be automatically done by assets and xcode.
check out this
iPhone 6 Plus resolution confusion: Xcode or Apple's website? for development
When i started working on my game app i didn't know i should create retina/non-retina #2x, #3x, #1x images. I just created the images with no #1x, #2x, #3x at the end of png file. So since my images are currently called image.png, it's considered a non-retina image i know now. Example is I made an image 25x35 in png. In Storyboards i decided to resize the image and i made it 35x40 because it looks better for iPhone5 than 25x35. My issue is even for 1x i'd have to re-create 40 images from scratch for #3x first and then use Prepo app to automatically downsize to #2x #1x scales. I tested my non-retina images on iPhone 4s/5/5s/6/6+ simulators and iPad 2, Air, Retina simulators and they all look the same and great, and app works well. No blurs or issues. Can I just use my image.png for Retina device as well? Would Apple allow this?
If this is an in-app image, and not the application icon, then yes, you can just use Any resolution image. You can use UIImageView and set contentMode on it to perform AspectFit, ensuring the image will be the same size on every screen type, and also will not get stretched.
myImageView.contentMode = UIViewContentModeScaleAspectFit;
Example: The simplest way to resize an UIImage?
This will preserve the aspect ratio of the image when fitting it to the image view container.
I would provide the image itself with the highest resolution required (#3x), but without #3x in the name, and let UIImageView scale it down to the required size for the other two resolutions.
There is a performance hit in doing this. If it doesn't degrade your scroll performance, go for it.
So basically: start with a high res image and scale down. Do not scale up a low res image, because it won't look crisp. And keep the same aspect ratio so it doesn't stretch.
You will not get rejected for doing this to in-app images.
If all of your assets have the #3x postfix then the App Store recognizes your app as being optimized for the iPhone 6 (together with correctly sized startup images or .xib / .storyboard). Using bigger images for non-retina or normal retina screen causes your RAM usage to increase and slightly taxes your GPU more but for simple apps this isn't a real concern.