I am using tabcontroller. In the first image screenshot got from storyboard, the second one got from the app on the iphone. Once you look closer to the images in the second image, you could see images are cut at the bottom compared to first screenshot tabbar images. I do not have any idea why this happened.
Make sure you use the correct names as of filename#2X.png for Retina and filename.png for non-Retina images.
Make sure you always use filename.png name convention and not the #2x in code.
Make sure you use the right dimension. My UITabBar images are some 39x48 pixels for Retina on the iPhone.
I would also advise you to convert to asset catalogs as they are enormously easy to use.
Related
I'm struggling with understanding how images and their different sizes/resolutions work in ios development.
Do images that will be used for buttons always have to be squares (like App Icons)? and are there specific dimensions that they would need to have?
Also generally speaking, how should images that would be used for buttons be sized in the Assets.xcassets folder as compared to Images that would be shown as regular images? (that is if there is a difference)?
Since you can create buttons of any size you like there is no difference. Your image should match the aspect ratio of the button but it's all entirely up to you.
Sizing in the Assets catalog would be the same as any other image with 1x, 2x and 3x variations.
Try it out in Storyboard and you will see immediate results.
I want to set a background image to my app but the image is not centered. The resolution is 640x1136, I have only one size, do I need more ? What is #2x and #3x ?
EDIT
I ended up spending ~ an hour in chat with the OP, and the solution to their problem was ultimately to remove all launch screen assets from the xc bundle and instead just put a UIImageView on the launch screen storyboard, anchor each side of it to the sides of the view, and set fill mode to aspect fill. That said, the below information is more likely to solve the problem for the majority of people who run into this, I think.
You need different sized images for the different screen sizes & aspect ratios. From the Apple docs:
It’s best to use an Xcode storyboard for your launch screen, but you can provide a set of static images if necessary. Create static images in different sizes for different devices, and be sure to include the status bar region.
The documentation includes a large table with all possible screen sizes one might support.
Many tutorials exist on the topic, and they may be a good place to start. This similar SO question also has a number of useful and informative answers.
Personally, I make one high-resolution image and then drop it into a tool which automatically generates all necessary screen sizes for me. I do the same thing for all the app icon sizes. Many such tools exist; SO is not an appropriate place to pontificate on which tools are better than others, so I will leave that choice (and research) up to you.
As far as where to put the images, the following instructions from Matthew Palmer's tutorial should be sufficient to get you started:
1 In Xcode, Click on your Assets file (Images.xcassets) in the left sidebar. It should open in the main pane.
2 In the left sidebar of the main pane (where AppIcon, LaunchImage, etc. are), right click
3 Select New Launch Image
4 Add your new launch images as normal. If you want something quick and easy to use, Paul Haddad recommends taking a screenshot in the iPhone 6 simulator and using that, or DavidSmith has created some default ones.
5 Change the Launch Images Source from your Target’s settings.
I found a useful list of launch screen resolution sizes here, which I will quote below:
Image Resolution Image name
320x480 Default.png
640x960 Default#2x.png
640x1136 Default-568h#2x.png
750x1334 Default-667h#2x.png
1242x2208 Default-736h#3x.png
768x1024 Default-Portrait.png
1024x768 Default-Landscape.png
1536x2048 Default-Portrait#2x.png
2048x1536 Default-Landscape#2x.png
The link where the above table was quoted from is also probably the best tutorial I was able to find on the topic. Note that, if you make the larger sizes first and drag them into Xcode, Xcode and auto-generate many of the smaller resolutions for you. (For example, providing one iPad resolution will typically be sufficient for all iPad resolutions.)
Ok let's take this one question at a time...
Background Image not being centered, I assume you have an ImageView containing that image... If so you should be able to do something like imageView.contentMode = .center
The #2x and #3x are for the newer phones with higher resolution. It is advised to have all 3. Also if you could show any code that would help.
I am using xcode with Swift to develop a very simple application that should only run on iphone7. I have a bunch of UIImages and UIButtons with images in my application and I have received a bunch of PNGs to use in the application.
They are named more or less image1.png, image2.png and so on.
If I check the dimension of the PNGs, they look right to me.
For example one background image has the following dimensions: 750 x 1335.
Once I add this PNG to my application, and add it to the story board, the image looks huge.
I need to manually enter the following dimensions 350 x 887 (which is half the size) in order to get the image to fit the size of the phone on the screen.
I have no idea what is going on here! It is something I am doing wrong or it there something wrong with the exported PNGs?
I think you content mode is not right. This is how to fix it:
Click to your UIImageView
Look at the right hand side and choose View -> Content Mode to Aspect Fit
This is the document about content mode of a Image View
https://useyourloaf.com/blog/stretching-redrawing-and-positioning-with-contentmode/
I hope it can help you
iPhone's physical screen resolution is rarely referenced as is, instead it is calculated in points. On the early iPhones 1 point was actually 1 pixel. On modern iPhones 1 point is actually representing 2x2 pixels, or sometimes 3x3 pixels (for Plus models). To make it all work, all the graphics for UI elements (buttons and the like) are provided in two or three resolutions:
for the case where 1point = 1pixel
in such case the image name is not postfixed with anything (image1.png)
for the case where 1point = 2x2pixels
in this case the image name is postfixed with #2x (image1#2x.png)
for the case where 1point = 3x3pixels
in this case the image name is postfixed with #3x (image1#3x.png)
You have two ways of managing this.
First goes like this:
Rename all of your images, so that they all have #2x.png at the end. Like this:
image1.png becomes image1#2x.png
image2.png becomes image2#2x.png
Drag all those images to your Assets.xcassets folder in XCode:
Now you can assign the images to buttons, backgrounds, and pretty much anything else by calling its name (omitting the #2x at the end), like this: image1, image2, etc. The resolution will fit now.
You can also choose the images in drop-down lists in your Storyboard to apply them to different elements (like said buttons).
Second way is similar.
You don't rename the images, and drag them into the Assets.xcassets folder just as they are. Then you manually drag the thumbnails of your images into 2x slots. Here's an example with an image called 'gear':
For more information on how physical screen resolution of the iOS devices differs from its internal representation (which is in points), refer to this handy guide: https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions
I am trying to add a full-screen background image for different sizes of iPhones as shown below.
I am thinking two options:
(1) I will make 4 images for each of the screen (640x960, 640x1136, 750x1334, 1242x2208)
(2) I will make one image (1242x2208) that has bleeing areas to cover all 4 sizes.
For option (1), since the background image looks the same, is creating 4 images necessary (app size increases)? For option (2), is it a correct way? I have not found anyone talking about this method while researching online.
Which one should I go for? Or better alternative?
As it is a background image, I suggest making a large image. Make it the same ratio as the small iPhones. Then use either ScaleAspectFit or ScaleAspectFit for the 6+. However, do not scale the image up to the 6+. Good luck!
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.