Change UIImageView height while scaling down to specific width - objective-c

I have a UIImageView inside a UIScrollView.
What I am trying to achieve is to have a flexible height for the UIIMageView depending on the image that is loaded in while keeping the width of the UIImageView the same.
I also don't want the UIImageView to scale from the centre, but rather from the top most point.
Any help is greatly appreciated.
Thanks,
Ashey

First of all, adjusting the height of a UIView (or any of its subclasses) will adjust the height from the origin.y coordinate downwards. It doesn't span from the center.
Do you know the sizes of your image beforehand? If the images appear in the same order each time, you can just supply the sizes in an array.
If you do not know the sizes, or the order of the images, you can use the size property on the UIImage you are putting in your UIImageView. This will return to you a CGSize, of which you can take the height. You will also need to update the contentSize of the UIScrollView.

Related

UICollectionView: What is the difference between contentSize and bounds?

What is the difference between:
self.collectionView.contentSize.height and self.collectionView.bounds.origin.y?
The latter always seems to be smaller than the former?
Any clarifying sketches would be highly appreciated.
contentSize is the size of the content, which is shown by scrolling. say you have 10 cell's sizing of 3600x1200. Then the content size will be 3600x1200. Bound will be the actual frame of the collectionview.
For more information
contentSize is not direct property of UICollectionView. It's derived from the UIScrollView. check here
happy coding.
The value of self.collectionView.bounds.origin.y is 0. Because the bounds of an UIView is the rectangle which is expressed by its own co-ordinate system.
Refer to Cocoa: What's the difference between the frame and the bounds?
self.collectionView.contentSize.height = contentSize of scrollview
Let you have a collection view(scroll vertically) whose frame is (0,64,320,504) and size of its each cell is (100,100) and you have 20 item. So, each row of collection view contain 3 cell. Now, to fit 20 item in collectionview 7 row will generate and we can see all those item by scrolling vertically.
Actual size of collectionview is (320,504) and the contentsize is (320,700).
Note:Assuming collectionview is scrolls vertically and there is no space between two row.

UIImageView fit to View frame size

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

How can I trim a UIImageView to fit an aspect ratio image

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.

Autoresize Height and Width Proportionally Interface Builder

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.

Make NSView resizable WITHOUT resizing images contained within

I have an NSView that resizes automatically based on the size of the window. I load images and display them in the NSView, but I do not want the NSView to try and "squeeze" them to fit the NSView's size. What I would like is to have the images load into the NSView at 100%, then resize the window to reveal more of the image rather than scale it to fit. I may want to modify this later to allow zooming and panning as well.
Here's what I did:
-(void)drawRect:(NSRect)rect
{
NSRect theRect = NSMakeRect((rect.size.width/2)-(([theImage pixelsWide]/2)/2),
(rect.size.height/2)-(([theImage pixelsHigh]/2)/2),
[theImage pixelsWide]/2,
[theImage pixelsHigh]/2);
[theImage drawInRect:theRect];
}
How are you displaying the images in the first place? Either you're using an NSImageView or you're drawing the images into your custom view yourself in -drawRect:.
If you're using an image view, take a look at its autosizing settings (its geometry as its parent view's geometry changes) and its image scaling properties (the image size and ratio as the image view's geometry changes.
If a custom view then draw the image into whatever rectangle you please rather than the view's entire bounds.