UIImageView Content Mode for responsive image view frame - ios

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?

Related

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.

Swift: is possible to get the default size of UIImageView used by UIImagePickerController?

In my iOS app I am developing, it is necessary for the user to take a photo. As you can see from the following image
by default, when we take a photo with iOS camera app, then it is showed with a standard/default size (the one delimited by those 8 pins) and I would like my app to be the same. So, which size should I set my UIImageView into which the photo will be displayed? How can I get that default size in Swift?
Or maybe...which would be the best size to give to UIImageview to prevent the photo from being deformed too much?
Thank you very much for you attention
UIImage has a property called size which specifies width and height of the image. So you could size your UIImageView to those.
Alternatively, if you're using constraints or autoresize mask (flexible width, flexible height) simply don't set a size and the UIImageView will fill itself according to contentMode.
You must understand however that what you see in the image you posted is not the "original size" of the image. Someone decided that the UIImageView should be place at X distance from top and bottom margins, thus forcing an implicit size on the UIImageView

Framing an image in iOS

I'm working on an app using XCode 6 and I'm trying to frame an image. I have one image that will change dynamically depending on the selection in the prior screen. I have another image behind it that is literally an image of a frame. The idea is to have the actual image look like it is surrounded by the frame itself. Here's the trick. I want the actual image to be a certain width to fill most of the horizontal size of the screen (say 85% if you want a number). I want the image to resize to this width based on the width of the screen (based on iPhone 4 vs iPhone 6, for example; so the width sets based on the screen, and the height sets based on the width to maintain its original aspect ratio). The frame should be about 10 pixels wider and 10 pixels taller, leaving 5 pixels around each edge, and the pictures should be centered at the same point.
I've seen a few programmatic fixes for resizing things based on the original aspect ratio of the image. However, I've been primarily using just Storyboard and was hoping to get an answer along those lines. Thanks so much!
Use auto layout and add constraints to the left and right of the frame to the superview. Then add constraints to the image inside the frame to be 10 pixels on top, bottom, left, and right to the edges of the frame. The left and right constraints of the frame should leave everything centered on the screen. Do you have any experience with Auto Layout? It can be a bit of a difficult learning curve, but for your purposes its a perfect solution.

How to work with Ipad images? Xcode

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.

Any way of resizing UIImageView / Label Text Size based on iOS Device?

I am learning to use constraints in Interface Builder and have tried messing around with aspect ratio and width/height properties of UIImageViews. I was just curious if there was anyway to autoresize, sort of like "zoom" to prevent a lot of white space in bigger iPhones? (6 & 6Plus).
Example of what I mean:
Notice the bigger devices have a lot of white space, would be nice to simply resize that UIImageView up top along with label based on device, sort of like I did with that bottom button by using aspect ratio. Any ideas?
With IOS 8 apple introduced size classes as an addition to auto layout.
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/EnablingAdaptiveSizeDesign.html
With size classes you can have different user interfaces for different devices and different device orientations.
For The case you are describing (dynamically resizing of Views), you can also just go with the "old" basic auto layout functionality:
Set Constraints of UIImageView to keep a fixed distance to the top
Layout guide
Set Constraint UIImageView to keep a fixed distance to your top UITExtField
Center UIImageView in Container
Set Fixed aspect Ratio of your Image
Tadaaa, you have an dynamically resizing Image that will always keep its aspect ration.

Resources