I have a UIView of size width: 285 and height: 243. Within that view, I have a UIImageView (it is within that UIView according to Storyboard). That UIImageView has width 283 and height 241 (so that the UIView shows 1px around). I position that UIImageView 1px vertically and 1px horizontally from the UIView. Now, for some reason, everything shows fine within Storyboard, but once it runs, the UIImageView doesn't respect any of the height or width I gave. IF I set the mode to Scale to fill, it works, but I want Aspect fill which it doesn't work.
I am using auto-layout by the way. Any ideas?!
You haven't said what about it "works" so I'm going to guess.
Aspect fit, by its definition, will likely show a different border on 2 edges because it has resized the image for display. If your aim is to put a border around the image then you should:
Remove the container view (it offers no benefit)
Add a border to the image views layer
Calculate the aspect ratio of the image
Set the image view frame size to fit around the image
A number of options are described in this post: How to scale a UIImageView proportionally?.
Related
When I create a new ViewController and I drag a UIImageView to this, the UIIV grows to fit fullscreen with the width and height from the screen. If I change between 4 and 3,5" screen, the UIImageView changes his height dynamically.
BUT, when I try to insert an UIImageView to a view with more elements, this UIImageView is setted with 4:3 ratio size, and if I try to resize this to fullscreen, when I change between screen sizes, a part of the imageView is hidden.
How can I fix it?
Short Answer: use Auto layout
Long Answer:
select imageview, add left, right, top, bottom constraints (all equal to 0) to tableview
select imageview, update frames
I am using a crop tool in my app and I need to modify a UIImageView so that it fits an image exactly after inserting the image in aspect fit mode.
So an image is selected and added to the UIImageView in aspect fit mode. The problem is that this then leaves "blank space" around the image inside the UIImageView that needs trimming. I was wondering how I could then go and resize the holding UIImageView based upon the image inside.
Is this possible?
The Easy way is simply using the following code on your "imageView"
imageView.contentMode = UIViewContentModeScaleAspectFit;
Assuming you want to cut a "zoomed" section of your image to fit fully into your imageView
check your original image width and height
Assuming width is bigger in size then height , scale the image width to the holder width
center the image on your holder , the width will fit perfectly (section2) and the height will simply be cropped follow above and below the holder.
It turns out that a better approach is to use the following idea.
How to get the size of a scaled UIImage in UIImageView?
Instead of trimming the UIImageView, insert the image and then get the dimensions of the image inside the UIImageView, from there you can then resize the UIImageView to match the dimensions of the image inside.
I have a UIImageView that is set to autoresize it's height based on the height of the Superview, i.e. when the In Call Status bar comes down. How do I make the entire view resize proportionally so that the width of the UIImageView changes when the height changes?
I would like to do this in Interface Builder, but programmatically can be used as well.
Thanks
The easiest way in iOS 6 is to use the (new in iOS 6) autolayout feature. It is very easy to make a view's width always be a fixed proportion of its height.
Otherwise you'll have to detect the change in your view controller's layoutSubviews and use code to resize the UIImageView.
However, consider the alternative of letting image resize rather than the whole image view. If you give the UIImageView the right contentMode, it will automatically resize the image proportionally if the view's height changes.
I have a UILabel that I am trying to rotate. First I am calling sizeToFit on the label, adding a little padding to the frame and then setting the transform to CGAffineTransformMakeRotation(rotation) (where rotation is a value calculated from a UIRotationGestureRecognizer).
Some of the text is being clipped as the UILabel is rotated (as illustrated in the image linked below). For demonstrative purposes, I made the UILabel's background color red so you can see how the bounds change when it is rotated. How do I prevent this from happening?
http://cl.ly/image/3F0M420R0d14
Thanks.
I have a simple test app on which my rootViewController's UIView contains a bunch of UIView subviews. Each one of those UIView subview is backed by a CAShapeLayer.
I want the composition created by those subviews ( the four shapes that are within the dotted area .. ) to always stay vertically and horizontally centered with respect to my
UIWindow. (the minimium size of the left/right, top/bottom margins will be subject to be changed at runtime at each orientation change )
So for example when i rotate to portrait i will have to
resize and reposition those single shapes so that the whole figure will be mantained centered and each CAShapeLayer sublayer stays sharp ( i want their path to be resized not just a raster resize )
what would be the best technique to resize/move the shapes to always have a centered composition while maintaining path crisp appearance for the shapes?
Ultimately for me it will be good to have an answer to this: how can i shrink the subviews as a whole? i mean their sizes and relative positions?
Thanks
You can use CGPathCreateMutableCopyByTransformingPath() or -[UIBezierPath applyTransform:] to recalculate all points in the path.