Stretched Image With Correct Constraints - ios

I've recently started a tug-of-war style game but in a single view application. I have a circular image over a resetScore button and all is well until I try to run the application. When I do so the image is out of the perfect circle form and stretched to a vertical oval. I have centered my image in the UIViewController but that has not worked. With one of the reset variations, I set the aspect ratio of the image to that of the background and then I set the image to the aspect of 1-1 and the image came out in the right proportions on the simulator but then I tried that with one of the other reset variations an it did not work like it had before. If anyone knew of a solution towards this that would be much appreciated.

Related

Need help in position images when view going back into place using

Hey everyone I've been stuck on this issue for the past few days. It's pretty hard to describe so I uploaded a video for it on a GitHub repository:
https://github.com/DJSimonSays93/PresentationController
It involves custom view controller transitions using present and the delegates UIViewControllerTransitioningDelegate and UIViewControllerAnimatedTransitioning
The "problem" is straightforward, now let me see if I can describe it in words.
You've got two image views showing, say, the basil image: the one in the scroll view, and the full-screen one when you present. They are both set at Aspect Fill. But the aspect ratio itself is different for these two image views. Therefore, in order to be displayed in Aspect Fill in a different image view, the image shifts in the way it is shown.
The reason you find this upsetting at the end of the dismiss is that you don't notice it happening at the start of the present, but if you slow down your video (you can turn on Slow Animations on the simulator, it will help), you can see it there too:
When you tap an image in the scroll view, there is a jump as the image changes its display for the new image view, and then the image view grows to its true size as the transform is removed (animated).
When you tap the full screen image, the transform is applied (animated) and then there is a jump as the image view is taken away to reveal the old image view that was behind it.
If you make both image view Scale To Fill, that won't happen. Of course then there is a distortion of the image, but it is the same distortion so that the image appears to grow and shrink smoothly right the way through. Try it and you will at least be satisfied by the sense of what I'm saying.
(See https://www.apeth.com/stretch.gif.)
If you don't want to do that, then you will have a lot more work to do; you will need to figure out how to actually compensate for the difference in aspect ratio by changing the aspect ratio of the image to match. Or, you could make the goal size of the fullscreen image view match the aspect ratio of the small image view (and part of it will just end up offscreen).
The point is, you cannot display the same image using Aspect Fill in image view with different aspect ratios and expect the image to appear the same way in both. Either the image needs to be different (to compensate) or the aspect ratios need to be the same.

XCode12: How to set up constraints in an image to make it responsive to an screen?

I'm having issues with constraints in XCode 12: Even if I describe accurately all measures involved, there is no way apparently to resize a 850x600 image to make it fit proportionally in the screen of an apple device. As you can see in the image, the constraints are defined to make the original image fit WITH BORDERS on the Iphone's screen, but nevertheless is turns out to be a disaster afterwards...
As you can see in the screen capture, the image overflows the screen when the model device is changed. So...we have a problem. And the answer or clue that I'm seeking night and day is...
How to make this image to become RESPONSIVE to screen size, and keep it's proportions. No way how the hell something so basic is so difficult in XCode...
must edit... look how the alignment constrainst are shown in grey...no idea why...this avoids me to try the solution provided by the good fellow above...
It seems you've defined constraints to center the imageView and having it a size of 850x600 pixels. This will keep the image of that exact same size, regardless the device it is shown on.
If you want the image to shrink, keeping the aspect ration you need to define constraint for the top, bottom, leading and trailing edge.
And you need to set the imageView's contentMode to "Aspect Fit", which is the default, afaik.
This is an image shown on an iPhone 11:
And this the same image shown on an iPhone 4s:
Edit:
One way to add those constraints is to select (highlight) the imageView and click on the button with the little square in the middle surrounded by those "T"s. :-)

scale type or contentMode of Image in UIButton is not working

I was trying to put an image in UIButton and the image would fill up the space keeping the aspect ratio intact. I was not able to apply the content mode. Please see the screenshot
I also tried from the code and it's not working as well
myButton.imageView?.contentMode = .scaleAspectFill
By changing the alignment, the image takes the whole space but it does not keep the aspect ratio and content mode does not work as well.
What's causing your problem is how small the image is. There are two things you can do. One option is to set the image you have now as the background image and then change the button's constraints to get the proportions right. You won't be able to have the button be the size you have it now, though, because the image will be all stretched out.
The other thing you could do is find a bigger or different image of that symbol (square.and.arrow), rather than using the one already loaded into Xcode.

UIImage being stretched when copying from UIPasteboard

I have a valid image with the right content size, however I have noticed when I try to paste this image to WhatsApp or through iMessage the image get's significantly stretched. I have looked at the image and it seams to be fine on UIImageview, but somehow it gets stretched in particular to outer controls.
Is there anyway to fix this issue?
When copying an image make sure to modify the imageView's mode. There are different modes, like Aspect Fit, Scale to fill and so on.
If let's say the image is 500x300 and your imageView's size is set to 350x230 and mode to Scale to fill, it will end up stretched. Try to fiddle around and see what works for you. Most of the times i choose Aspect Fit.
Try that out.
BTW. You can find it under inspection tab in design editor or set it programmatically if you choose so.

iOS Aspect Fill Image Resizing with Content Aligned to Left/Top Edge

I am developing a universal app for iOS-7. this particular question is specific to iPad only. I want to place an image as background of root view of a View Controller i.e. the image should fill the whole screen. The 1x image has size:768x1024. This works for Portrait orientations as non-retina resolution of iPad is also 768x1024 in portrait. In Landscape however the image does not fit. I have tried using ScaleToFit but since aspect ratio of image is not preserved I can not use ScaleToFit(their are things in image which look odd when not scaled proportionally in both axis). AspectFill resizing seems most suitable for my need, but their is a small problem. As defined in Apple Documentation "The content is resized to completely fill the bounds rectangle, while still preserving the aspect of the content. The content is centered in the axis it exceeds.". I do not want the content to be centered in axis in which it exceeds, I want it to be aligned to top/left edge.
So basically I want two things:
Aspect Fill
The content remains aligned on the Left/Top edge.
Is it possible to achieve this. Any code-snippet will be great.
Thanks
You will have to subclass the View Controller's view and manually scale and align.
Look at the code snippets in THIS answer.
Adjust imageViewXOrigin and imageViewYOrigin to align however you want.
Really the only way to get around the content fill mode is to have two different images, one for each orientation. I'd suggest changing the image in the view controllers willAnimateToOrientation: method so that you can put the image changes inside UIKit's animation block.

Resources