How to work with Ipad images? Xcode - ios

I am creating an Ipad app and no matter what height I set my ImageView to be it is always too big.
For example I have an ImageView that's about 20 pixels tall and has all of the constraints that I need to keep it where I want it to be. When I play this 20 pixel tall ImageView on the Ipad simulator the ImageView covers about half of the Ipad screen.
How can I work with Ipad images if even a 1x1 pixel image view takes up a signifiant amount of the screen? (Yes, I made the 1x1 image just to see if it would still be insanely large)

Under Auto Layout, if you don't set the size of the image view absolutely, it will size itself to match the size of the image assigned to it. That is probably what is happening to you.

Related

Same image slicing displays very different on 8 and 8 PLUS

Here's the Sample Project
Problem
I want to make an image to have a fixed top half part and a stretched bottom half part which to make it properly resized in any height.
And I use image slicing in Assets.xcassets to achieve that. The image can be resized properly, however, it displays slightly different in different sizes.
The Original Image
Image Slicing
Just simply slice the image from the vertical center.
Storyboard
Content Mode is set to Scale to Fill
Height constraint is equal to its width constraint multiplied by 0.8
Result
I think it should display nearly the same images in different devices. Because everything is the same except for the image assets(2x, 3x).
However, the result is not what I expect. The image seems a bit squashed on iPhone 8 PLUS.
At Last
I'm using Mojave with Xcode 10.1 (10B61).
Anyone knows what is the cause. And how to make the image vertically stretched properly?
The reason it looks "squashed" is because the image is being stretched horizontally.
Your slicing allows the top portion to remain a constant height, but not a constant (or proportional) width.
Here, I use your original image without any slicing... each image view is the same height (201-pts), so you know it's not scaling vertically, but different widths so you can see the result of horizontal stretching:
What you may want to try is clipping your original image to only the top portion. Setup the constraints on that image view to stay proportional, and then use a solid color image view (or just a UIView with matching background color), as the lower "vertical stretch" section.

How to size images on iOS devices?

I'm making a spritekit game. I understand how to size my images in an SKScene because it's 750x1334 points across all devices, but the main page of the app uses UIKit/a collection view controller, so I'm not sure how to size my images. I want to have the images take up 1/3 of the screen width and then have the image height equal the image width.
iPhone SE is 320x568 points
iPhone 7 is 375x667 points
iPad Pro 10.5 inch is 1112x834 points
Do I just make images that are 1/3 of the largest possible device width (1112 points) and then let smaller devices scale the images down? i.e. 1112 points divided by 3 is 370 points, so make images that are 370 points wide and high (740.6px#2x and 1112px#3x)?
I want to have the images take up 1/3 of the screen width and then have the image height equal the image width. Do I just make images that are 1/3 of the largest possible device width (1112 points) and then let smaller devices scale the images down?
No. When you "let smaller devices scale the images down", you are still loading the full-sized image and thus wasting memory. Plus, letting the runtime scale the image down automatically (e.g. because you use an image view with an aspect fit or aspect fill content mode) wastes time and processing power, often at a critical moment when the app is trying to launch.
The correct procedure is:
In your app bundle, you should include just one size for the image, possibly in three resolutions, and this should be the smallest size that is acceptable on the largest screen you'll be showing on.
In your code, you should figure out the size at which the image will actually be displayed for the screen of the device we are running on, and load a thumbnail of the image sized down to that size.
In this way, no memory is wasted; the image held in memory is the image as displayed.
In Storyboard, you can use the below steps,
Put equal width and equal height between image view and your actual view
Select the equal width constraint and change the multiplier from 1 to 1:3
Select the equal height constraint and change the multiplier from 1 to 1:3
The image view should occupy 1:3 of your screen size now.
Screenshots are attached for iPhone and iPad designs,
Hope it helps.

UIImageView Content Mode for responsive image view frame

I have an image thats a particular height and width and its designed in such a way where its content is meant to be displayed at a minimum height, and as the height increases more of the image is allowed to be seen (meant to clip at the frame). This is because we wanted to serve up one image, but depending on device size, the frame can get display as much of the photo necessary.
My question is whats the best way to manage the content mode of the UIImageView to allow for this as well as how should the imageview constraints should be managed. Any advice? My original thought was to have the content mode be 'center' and use a combination of greater and less than height constraints to achieve the height part.
The red rect shows the uiimageview frame at its small screen size (iPhone SE), green is at normal size (iPhone) and orange is for plus devices.
Perhaps the community has a better solve for this?

image background files for iPhones

We have a background image for our app that needs to be full screen for each device we run the app on. Our problem is the background image is tiling on our iPhone 6S+ (Display Zoom off).
I have drawn in red lines to highlight where the tiling is occurring...
We have created 3 background images of the following sizes...
So, designing for 1x (which is the recommended way to go), our base level 1x background image is 320 pixels wide. Our 2x is 640 pixels, and our 3x is 960 pixels.
The problem is the iPhone 6S+ is 1080 pixels wide and according to this chart, you need to start with a 3x image that is 1242 pixels wide. And this is where I am missing how this is supposed to work.
from https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions
With the above chart in mind, it seems you need a separate image for each resolution highlighted with a red square in the above image. Is this correct? And if yes, how do you label each individual image so that at runtime the correct one is picked?
Three images, named as you have them for background.png, are all you need.
Now let's talk about image views. They display their image using a content mode. The key thing is to pick the correct mode. Aspect Fill is what you probably want here, because it will fill the image view without distorting the image.
One procedure, then, is to use a bigger image than what you have, and configure the image view that shows the image to use an appropriate content mode such as Aspect Fill, so that it sizes the image down to fit (or, to save memory, at runtime you can size it down yourself).
The other possibility would be to leave your image as it is, and solve the issue on the Plus machines by telling the image view to size the image up to fit, again possibly by using Aspect Fill. That might or might not look acceptable; you'd have to try it and see what you think.

How can I use UIImageView with aspectFill and alignment to the top?

How could I use UIImageView with aspectFill and alignment to the top? Auto-layout or programmatically don't mind.
It can be done, but you are talking about quite a bit of coding. Let's start with a detailed description of what you are asking for.
We'll start with an iPhone6s in portrait mode with a screen size of 667x375 points, or a regular height by compressed width in size classes.
(Keep in mind, on any iPad you'll also need to work with slide-over and split screen too.)
But for now we'll go with 667x375. Into that we'll add our imageView as a square size of 375x375. Along the top of the screen when in portrait mode.
(Keep in mind that with AutoLayout you'll also have it pinned, and without putting some code in to determine if it's in landscape or portrait, otherwise, that 375x375 will shrink in landscape if you have other views underneath it.)
So now, stripped out to the most minimum (for now), we are talking about how to pin "an image" to the top of a UIImageView that is 375x375 in size and with a contentMode of aspectFill.
We need to find the size of the image next, based on both the largest dimension (height or width) AND how that dimension compares to the size of the imageView, calculate how the imageView will normally render it. The key figure you want is the height it will render it as
For instance, how will a 480x1020 (HxW) image fit into it? The 1020 width will be taken down to 375, so the height will be 480/(1020375) or 176.4705 points.
Always do the math for both dimensions - if your imageView isn't square or if the height isn't the "bounded" dimension, you'll be off.
Once you've calculated what the "Aspect Filled" height will be, check it against the dimensions of the imageView, and adjust the height of it as needed.
Here's where the real work comes in. If you are using AutoLayout, you can do a heightAnchor adjustment, but depending on how you pinned those other views things may be laid out really bad after you do. If you are using frames and CGRects be aware that you have to take into account iPad slide-out and split screen, etc.
Finally, if you want your app to load a different image in that imageView after it was loaded, you will have to "reset" the height back to what it originally was, at least in your calculations.
My recommendation is to find an alternative to what you are asking. For instance, put a small border around the image view to let the user know the true dimensions of that view.

Resources