My application, which should support iOS 5.0+ I have background UIImage, which should be load proper image depending on device. So I've created 5 images with corresponding resolutions.
img.png
img#2x.png
img-568h#2x.png
img~ipad.png
img#2x~ipad.png
Also I've set Default-568h#2x.png splash image. I then detect in viewDidLoad which device my app is running and use
[bkgImageView setImage:[UIImage imageNamed:#"img.png"]];
to load appropriate image. All resolutions are loading just fine except iPhone 4" one, which is img-568h#2x.png. If I load this one explicitly
[bkgImageView setImage:[UIImage imageNamed:#"img-568h#2x.png"]];
the scale is not correct. So how can I load 4" retina image automagically or manually?
Adding -568h to the end of the file name doesn't work. The only supported modifiers are #2x,~ipad, and ~iphone. The only case where -568h might seem to work is the launch image, where the image must be specifically named Default-568h#2x.png.
You should use auto layout (or struts and springs) to adjust your screen accordingly.
Your problem is that it's automatically stretching an image that's already large enough.
The solution:
If you load the image explicitly for that device, you should load "image-568h", and let it automatically add the #2x. That way, it knows that it's a high resolution image and doesn't attempt to stretch it.
Additionally, rather than detect the device, you could design the image with the extra space at the bottom to fit the 4" screen, set the image to Aspect Fill, and let it clip for smaller devices.
Related
I want have an image on my launch screen which occupies the same amount of the screen on different devices.
At the moment I have a logo which is about the right size on iPhone 4s, but on iPhone 6 Plus it's too small. Obviously these devices are in the same autolayout class.
I know I could use proportional sizing, but this will stretch the images and make them look fuzzy.
Also with it being a launch screen I can't use code for drawing or custom controls.
I guess I need some way to determine different devices in my constraints.
Add LaunchScreen asset group to your Assets. Then it will be possible to assign custom image for each screen resolution.
I am currently developing an ios app and the scope is limited to iPhones only and portrait mode only.
In each screen there are images, buttons, labels and textFields. The question here is, how do I render appropriate image for device from the Images.xcassets?
What I am doing now, written below -
From Sketch design application, exporting my images for 1x, 2x and 3x (Screens designed by a UI designer)
Adding them to Xcode project (drag and drop from finder to xcode)
Add new image set in Images.xcassets (naming it as "MyImages") and drag drop my images for 1x, 2x, 3x.
In the storyboard, on a viewcontroller, adding an ImageView
For the image view, selecting Image as "MyImages" from Attribute Inspector.
Once the above steps are complete, when I test the app on simulators starting from iphone4s, iphone5, iphone5s, iphone6 and so on.. (all the simulators available on Xcode 7.3), I don't see the appropriate image is being rendered.
Is my approach correct?
Also, how do I debug a UI element on the screens? Like, how/where do I check for what image is rendered? Its size (W X H)?
Added Screenshot
#Lohith Korupolu:
The screen shot you provided is for universal size (iPhone/iPad). From the additional information provided in comments, this would lead to issues with AutoLayout constraints that would stop the image showing on screens of a smaller size than that shown in your StoryBoard.
E.g. I have replicated your issue on storyboard...
This results in the following in Simulator for iPhone 4s....
i.e. Text is there but NO IMAGE.
REASON: The autoLayout Constraints set for the larger "Universal" Screen on any of two opposite sides would make the image invisible/ disappear when viewed on a smaller screen.
SOLUTION:
1. Clear the Autolayout constraints for this selected Image in Storyboard. See next picture...
2. Add AutoLayout constraints as below x2 pictures. (Top constraint, Height + Width) (horizontally in container). Remembering to tick "Items of new constraints" for both.
3. Run Simulator for iPhone 4s... E.g.
** The same situation is occuring with the other iPhone size simulator Runs. **
****** All Working ******
They should all be 2x images for iPhones apart from the plus size iPhones which should be 3x. Is that not what you are seeing?
In terms of debugging / checking this - you can put place holder images in your assets with labels or different colour tints to distinguish them from each other and then replace them later with images that give the desired final appearance once you know that everything is working as expected.
Using 2x images for iPhones approach works fine, however, there is another more straight forward way without having to resort to multiple image files for 1x, 2x, 3x in Xcode, by use of good large quality PDF (vector).
1. Create a Large PDF of the image/Graphic image you want to use
2. Import it into 'Assets.xcassets' - drag and drop (Into Xcode)
3. Go to the utilities panel on the right for the 'Attributes Inspector' (when the image is selected)
See Screen shot (a)
Under 'Scale Factors' the selection from 'Multiple' to 'Single Vector'
Now when you got to 'StoryBoard' and add the image - Simply select the PDF's name.
Xcode will automatically render it to correct size etc at run time for you. All the work is done by Xcode.
Exceptions:
It does not work well with images for icons inside the TabBar or Navigation Bar Items.
Note:
Vector graphics are sharp and ideal for High Definition (HD), but although Xcode accepts the Image Asset as a vector from the PDF, it doesn't seem to keep the vector but converts it into an actual image with pixelation problems when zoomed in, from a HD perspective.
I created an universal iOS app. On all iPhone variants I want native resolution without scaling, so I created splash screen images for all available sizes in images.xcassets/LaunchImage.launchimage and set them all in Xcode:
Now, this works very well for all iPhone versions up to the biggest iPhone 6 Plus.
On iPad (with high pixel density) though I don't want native resolution. Instead I want it to scale the app (even if it looks a bit blurry then, but that's ok).
How would I do that?
In image assets you have to provide proper size for each launch screen, You can not use the same image for two devices in launch-images ( unless you have two copies of the same image ).
If you want to make app-size smaller, implement launch screen, set its background color to your desired background color and put your logo in the center of it. You can now remove iPhone 6,6Plus images from image assets, but you still have to provide launch images for iOS 7 devices (old iPhones / iPad ).
Don't use the Asset Catalogue. Create a LaunchScreen.xib , add a UIImage inside the view. Open the attribute inspector in the right hand panel and set the view mode to Aspect Fill.
The solution was quite easy, I simply changed project type from "universal" to "iphone" and that did the trick!
I made a simple app using the storyboard feature. I have made some UIButtons and I want to change the background of my UIButtons. In the storyboard the background successfully changes but in the actual iOS simulator my background does not change.
Here are some suggestions for you:
If you set image programatically than check image name spelling, It should be match exactly with bundle image name(case sensitive).
You have to add two images in your bundle. e.g if image name is nature.png than you must have to add nature.png and nature#2x.png(#2x image must have double size than normal).
Although if you are using only one image for retina devices than check that you run your application in only retina devices, Sometime non-retina device won't show image.
If you set image as UIButton image with title than it is requited to set title and image insets, If title text is big or image size is bigger.
Weird problem here which may be a bug in Xcode...
Using an Xcode 5.1.1 storyboard with auto layout, I am working on the design of a view controller that contains a full iPhone 4 sized UIImageView that references an xcassets image set. The xcassets image set includes 1x and 2x iPhone 3.5-inch images, and an iPhone 4 R4 image.
Problem 1: Once I set the Image View Image for the UIImageView in the utilities panel using a View Mode of Aspect Fit, the storyboard shows the smaller iPhone 3.5 image with white spaces at the top and the bottom of the view instead of the iPhone 4 image which would fill the entire storyboard representation of the view, as they are the same size since Xcode infers an iPhone 4 sized screen in the storyboard.
Problem 2: If I ignore problem 1 and run the program using the iPhone Retina (4-Inch) simulation, the applications selects the correct image size from xcassets and everything seems fine. However, if I run the simulation using the iPhone Retina (3.5-Inch) simulation, the applications also selects the correct sized image, but pushes the images down and display white space at the top of the page.
Before I start hacking around these problems, I thought I would reach out and see if anyone else has experienced either of these issues. Any help will be greatly appreciated.