Image aspect ratio swift ios - swift

So, I'm learning iOS Swift. I'm going by some tutorials, and I'm stucked at image positioning.
I'm trying to figure out how uploaded picture is rescaled and positioned.Since I cannot post the screenshot, image that should be shown in my simulator as whole, I can only see like 25% of the picture. Should I change something in Attributes or in Size Inspector?
What I did so far (clearly wrong), was setting Intrinsic Size field -> select Placeholder, w/h = 320.
Then, I pinned and selected Aspect Ratio.
Any help, please?
Thanks.

If you're asking how to make sure an image fills a UIImageView:
myImageView.contentMode = UIViewContentMode.ScaleAspectFill
You may also need to set constraints if you placed your image view in interface builder.
It's not entirely clear from your question how you want it positioned, but hopefully these get you on the right path.

Related

Swift image Aspect Fill for tall image not working

I have this UIImageView which may contain images with different ratios.
I have studied the properties of UIImageView and it seems that the Aspect Fill property set for the Mode of the View should be what I need.
It works as intended when the image is wider than taller: cuts the left and right parts of the image out of the UIImageView.
However if the image is taller than wider it does not cut the above and below part of the image, it allows the image to come out of its view and overlap below views.
These are the constraints I have for the UIImageView:
I have seen several tutorials proceeding like I do, but the result is different for me.
If the image is fitting correctly, but is extending outside the bounds of the image view, try turning on "clip subviews"
For such condition , Two possible solution may help :
Calculate aspect ratio based on your required dimension so that picture quality remains intact . ( ** use Aspect Fit calculator to find required ratio)
CENTER OR ASPECT FIT attributs of Imageview
Thanks

Captured Image Squeezed After Taking a Picture : Swift

Short and simple, My captured image gets squeezed from both sides when loading it on the preview.
Long version: I am currently using the CameraManager Pod found at https://github.com/imaginary-cloud/CameraManager/blob/master/camera/CameraManager.swift. My problem is that after I take a picture, it squeezes the image a little bit on both sides. The width is not what I see. The bounds of my preview (what I see through the camera) is view.frame.bounds and my bounds of the imageView (what I just captured) is preview.frame.bounds. If need be, I can take a image of whats happening if you need more information.
If you need the code just comment and I can show you. Any feedback is greatly appreciated.
The problem was that I wasn't setting the contentMode of my image view. All I had to do was add this line
imageView.contentMode = UIViewContentMode.ScaleAspectFill
Duplicate of:
How to manage UIImageView content mode?
and
capture image is stretched using avcapture session
Sorry for posting a question without doing thorough research. I wasn't Google-ing the right query.

Why does my text look so bad in my ios App?

I have a uiscrollview that has a custom child view that contains text and images.
The text items are UILabels. I have attached an image representing the quality issue:
Do you guys think it might be worth a shot to draw the text, then convert to a uiimage and display that instead? Maybe it would scale better? Or perhaps there is a way to smooth the text?
There's no reason to expect that you're going to draw the text better than UILabel does. Your picture is very hard to make out, but here's what you want to look at:
Are you just adding a UILabel to the UIScrollView's contentView, or are you doing something else fancier. That "something else fancier" could easily be the problem.
Make sure you're drawing on pixel-aligned boundaries. The easiest way is to make sure the origin of your UILabel is integral (not a fraction). On a retina display, you can get away with half-points, but it's usually easier just to make everything integers.
I thinks it is the position issue. I bet you set the x or y value to float value like 12.3 or 12.5 .
What I have done now is I have created a uiimage version of my text and am showing that now instead of the uilabels, the quality is soo much better, you can really see the difference when you are looking at it scaled up on the ipad, it must just do a better job scaling images then text?
Before:
After:
The above images show how the text renders on the ipad with my app scaled up, before and after my change.

UI Button with Image Smaller than Touchable Area

I've not much response so am adding some more info.
My buttons are not rectangular, nor organised in a grid so I need a way of creating what looks like a button (and shows that it has been pressed visually, as per a standard UIButton) but where the touchable area is different to the image area.
I am using a transparent PNG and that element works fine. I've added the buttons in Interface Builder and am wondering if that is the problem.
However, if I change imageEdgeInsets, it distorts the image display, which is obviously not what I want.
Bizarrely, if I increase the dimemsions of the button, it doesn't change the image, but if I decrease them it does.
I have tried different combinations of mode (scale to fill etc), but to no avail.
I am aware that there is an image and background image property, but in IB there is only one.
Essentially, I don't understand how the geometry works and the Apple documentation doesn't seem to help.
Surely, I can't be the only person to try to do this. Any help would be warmly welcomed.
Many thanks,
Chris.
Try setting the buttons setting to Aspect Fit. This will fill the button with your image so a smaller image than the button size would leave the space around the image.
Also set the button type to custom.
In the end, I stumbled across Ole Begemann's Non-rectangular buttons class. It just does what I need - to be able to create buttons where the touchable area follows the visible element of a non rectangular image.
#Helium3 - thanks - that allowed me to use a larger touch area, bit not a smaller one.

Image "frame/window" on iphone

I have numerous photos in portrait and landscape format. I want to be able to display a "crop" of the photo in a UIImageView on the iphone. If the picture is in landscape format I want it to resize to fit the frame, if it is in portrait format I need it to resize to fit the width, and then be cropped at the top and bottom. As if there is a "window" over the image - so it looks like a landscape picture.
It would be even better if this could be a UIButton - although I am aware that I can use touchesBegan and such on images to make it behave like a button if needs be.
Thanks for any help you can give me.
Thanks
Tom
In an ImageView, you can change the View Mode to Aspect Fill. It will give you the right scaling/cropping you want.
For interactions, you can use a Custom button with no drawing at all (no Title, no Image, no Background) with the same size as your ImageView. That would be safe with regards to the image aspect. I've tried similar stuff using the button's Background or Image properties, it can have some undesired effects (I've ran into a resizing issue on iOS 3.1.3 for instance).
1°) To Crop the Image, try to add a method to UIImage class to do it (you can google it without problem, or even on StackOverFlow)
2°) To add a "window" over your image, just add an UIImageView over your image wich has transparency. It should work ;-)
3°) To know when an image is touched, you can use "touchesBegan" to detect which image were selected. But I think it's the last of your problems ^^
What you cant to achieve isn't so hard, just to it step by step !
If you want more help in one step, say it. But I can't code it all for you ;-)
Good Luck