How can I pin an UIImageView to the top without stretching it? - swift

I'm trying to pin an image to the top of the screen but I keep getting an extra white extra space at the top. I tried to pin the image relative to the superview instead of the save area but It didn’t work.
image's constraints relative to superview
changing the image's content mode to Scale To Fill solves the issue but it stretches the image.
After changing the image's content mode

You might want to change the contentMode of the UIImageView to either .scaleAspectFit or .scaleAspectFill, whatever fits best your needs.

Related

Trying to set a wide image in UIScrollview, with a fixed height

Novice that has been stuck on this problem for 2 days (please help!). I have an image that is much wider than the screen. I want the user to be able to scroll horizontally on the Image, and also want the image to be a specific height while maintaining it's aspect ratio.
Currently the UIImageView is nested within the ScrollView. The ScrollView has constraints to the Safe Area in all 4 directions, that place it in the upper half of the screen (250 from the bottom of the Safe Area). The ImageView has all 4 constraints of 0 to the "Content Layout Guide" of the Scrollview, and a 5th constraint of having an equal height to the "Frame Layout Guide" of the ScrollView.
I initially tried to set the image Content Mode to "Aspect Fit". This maintained the aspect ratio and set the image with the desired height within the ScrollView, however left a huge amount of transparency either side of the image so the user has to scroll for a while before seeing the image. For this I could not find a solution that contained the scrollview within the bounds of the non-transparent section only, so I then changed the image Content Mode to "Aspect Fill".
This removed the transparency either side but the image is now too tall and goes off the screen vertically. I have set "Clip to bounds" on the ImageView Size Inspector and also set it to true in code, but the image is still to tall and outside the height constraint specified. I also used:
gym_imageView.sizeToFit()
scrollView.contentSize = gym_imageView.frame.size
But none of this is fixing it.
My expected result was that the image with original dimensions (W: 12064px, H: 1696px)
Would resize itself to the constraint height (0 to top & bottom of Content Layout Guide of Scrollview, which in turn is constraint 0 to top of Safe Area, 250+ to bottom of Safe Area) while maintaining aspect ratio.
Any ideas?
The Content Mode changes how the image appears in the imageView, but it does nothing to resize the imageView itself.
You should set an explicit aspect ratio constraint on your imageView with a multiplier of 12064:1696. Then Auto Layout will be able to calculate the correct width of your imageView based upon the fact that it already knows the desired height.

Clipping issues when picking from uiimagepicker

I have been dealing with this issues for a few days now trying to figure out why images are clipping whenever I pick from the uiimagepicker. I have this button in my view controller that will take on the image that is chosen from the uiimagepicker. The image shows up but there is this weird clipping affect where it shows the entire image but there is this empty space that is black that makes the image look like it is being clipped.
The image of what the issue I am dealing with is below:
image of the current clipping issues
As now, your image view content mode is .fit that's why this is happening, For now, You need to set imageView content mode scaleAspectFill:
imageView.contentMode = UIViewContentMode.scaleAspectFill
imageView.clipsToBounds = true

UIImageView does not fit my screen properly

I have a UIImageView that fills up the majority of my screen, with the exception of a navigationBar at the top. On my storyboard, I am using the formatting mode titled "compact width|regular height" that is supposed to work for all iPhones in portrait mode. When I attempt to run my app on an iPhone 5c, with iOS8, the imageView does not fill to fit the screen, and appears to "spill out" of the screen, based on the size and positioning of the image within the imageView. I do not have any auto-layout constraints on my imageView, because when I attempt to put them on they enlarge and distort the image even further. I have tried Scale to Fill, Aspect Fill, and Aspect Fit, and none of those work. I have also made sure that the clip to subviews selection button has been checked, so that the bounds are clipped. If anyone can help me, I would really appreciate it. Thanks!
You can disable the auto-layout and use the autoresizing mask.
I used this on images and fixed the prob like yours that I had.

does UIViewContentModeScaleAspectFit scale image everytime leaving transparent area around

I am having a hard time understanding how the UIViewContentModeScaleAspectFit works . I am working on app in which i want that the image should be redrawn in such a way that there must be blank area around the image and the image must be set in the center of the image view.
My main concern is that will the UIViewContentModeScaleAspectFit scale image leaving an transparent area around (in every case) or do i need to redraw image programmatically.
If the background of the UIImageView that contains that image is white, then the areas not filled by the image will be white in that view.

Obj-C, how to handle a background image which is squashed on some views and not others?

I've got a background image with a radial gradient, on some views it looks fine.
But on others where I have extra controls at the top of the view its squashed as it has less room to be show.
I guess I'm going to have to take account of the where the images starts at the top or the bottom, so one end will loose a section. But if I can do this via a property, rather than stretch ? or if I have to chop the image some how ?
What can I do?
Are you showing with UIImageView? Default contentmode is set to UIViewContentModeScaleToFill, which is to stretch in all directions to fit frame.
Change contentMode property to get the effect you want, ie UIViewContentModeScaleAspectFill.