How to make UIImageView automatically resize to the size of the image loaded
The size of an image has no bearing on how large the UIImageView actually is, rather the size of the UIImageView solely depends on the size given to it in Interface Builder (or that you assigned to it). Else the images would be all whacky when you use the @2x images for Retina displays for example.
If you want to fix this, you must change the frame when setting the image as well. If you're doing this now:
[imageView setImage:[UIImage imageNamed:@"myImage.jpg"]];
change it to:
UIImage img = [UIImage imageNamed:@"myImage.jpg"];
[imageView setImage:img];
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,
img.size.width, img.size.height);
This will however not change the layout of view it is contained within, you can make it change the sizes of the other views automatically under iOS 6 using Layout Constraints. If you are an Apple Developer you can watch the WWDC instruction videos, they explain how that system works quite well.
If you're fine with the view not growing, and the problem is just how the image overflows it's bounds when you change it to one that does not match the dimension of the containing view, you can set the "Clip Subviews" checkbox in Interface Builder for the image view. This will make it so that the view will not draw anything outside it's own bounding box, if you also set the scaling mode to "Aspect Fill" or "Scale To Fill", the image will always fill up the entire bounds of the containing view.
How to resize UIImageView based on UIImage's size/ratio in Swift 3?
It looks like you want to resize an ImageView according to the image ratio and the container view's size, here is the example in Swift (Sorry,the former answer with a bug, I fixed it):
let containerView = UIView(frame: CGRect(x:0,y:0,width:320,height:500))
let imageView = UIImageView()
if let image = UIImage(named: "a_image") {
let ratio = image.size.width / image.size.height
if containerView.frame.width > containerView.frame.height {
let newHeight = containerView.frame.width / ratio
imageView.frame.size = CGSize(width: containerView.frame.width, height: newHeight)
}
else{
let newWidth = containerView.frame.height * ratio
imageView.frame.size = CGSize(width: newWidth, height: containerView.frame.height)
}
}
The simplest way to resize an UIImage?
The simplest way is to set the frame of your UIImageView
and set the contentMode
to one of the resizing options.
Or you can use this utility method, if you actually need to resize an image:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Example usage:
#import "MYUtil.h"
…
UIImage *myIcon = [MYUtil imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];
UIImageView resizes automatically
Your SimpleTableCell is a subClass of UITableViewCell
that you created? This imageView is a property of this subclass?
Just a guess:
Every UITableViewCell has a built-in imageView read-only property. In the code that you posted, you are using this default property, which I believe cannot have it's frame modified, it's kind of fixed in the left side of the cell.
Look for imageView property inside the documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/index.html#//apple_ref/occ/instp/UITableViewCell/imageView
So, if you do have your custom cell with your own imageView, make sure to not call it imageView
too, because you may end using the default UITableViewCell
's property, and not your own, which will have all your frame customizations and etc.
You should reference your cell by your customClass and call your property like this:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *userImage = [UIImage imageWithData:userImageData scale:self.profileImage.frame.size.height/self.profileImage.frame.size.width];
SimpleTableCell *cell = (SimpleTableCell*)[self.myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.myImageView.image = userImage;
}
Don't know if the problem is really related do this, so let me know if it helped or not.
With Auto Layout, how do I make a UIImageView's size dynamic depending on the image?
The image view's intrinsic size is already dependent on the size of the image. Your assumptions (and constraints are correct).
However, if you've set up your image view in interface builder and have not provided it with an image, then the layout system (interface builder) won't know how big your image view is supposed to be at compile time. Your layout is ambiguous because your image view could be many sizes. This is what throws the errors.
Once you set your image view's image property, then the image view's intrinsic size is defined by the size of the image. If you're setting the view at runtime, then you can do exactly what Anna mentioned and provide interface builder with a "placeholder" intrinsic size in the property inspector of the image view. This tells interface builder, "use this size for now, I'll give you a real size later". The placeholder constraints are ignored at runtime.
Your other option is to assign the image to the image view in interface builder directly (but I assume your images are dynamic, so this won't work for you).
Change UIImageView size to match image with AutoLayout
Auto Layout solution
Since establishing that you're using Auto Layout in your project, I have made a demo app to show you how you could change the image of the image view and adjust the height. Auto Layout will do this for you automatically, but the catch is that the photo you'll be using is coming from the users gallery and so they're likely to be very big and this.
So check out the app: https://bitbucket.org/danielphillips/auto-layout-imageview
The trick is to create a reference of the NSLayoutConstraint
of the height of the image view. When you change your image, you need to adjust it's constant to the correct height given the fixed width.
Your other solution could be to set a contentMode
on your image view, by using UIViewContentModeScaleAspectFit
your image will always appear in full but will be locked to the bounds of the image view, which can change based on the Auto Layout constraints you have.
Initial reply
It looks like you've really over complicated this, unless I've missed something.
When you get a new image from the image picker, all you need to do is change the frame of the image view according to the UIImage
size.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imageView setImage:image];
self.imageView.frame = CGRectMake(self.imageView.frame.origin.x
self.imageView.frame.origin.y
image.size.width
image.size.height);
[self dismissViewControllerAnimated:YES completion:NULL];
}
Of course this is going to be potentially very large (it's using the original image which may be very large).
So let's lock the width to 280 points... this way we can always have the full image on screen in portrait mode.
So assuming your image size is 1000x800, and our image view perhaps is 280x100. We can calculate the correct height for the image view retaining the image view's width like this:
CGSize imageSize = CGSizeMake(1000.0, 800.0);
CGSize imageViewSize = CGSizeMake(280.0, 100.0);
CGFloat correctImageViewHeight = (imageViewSize.width / imageSize.width) * imageSize.height;
self.imageView.frame = CGRectMake( self.imageView.frame.origin.x,
self.imageView.frame.origin.x,
CGRectGetWidth(self.imageView.bounds),
correctImageViewHeight);
Related Topics
Allow Uiscrollview and Its Subviews to Both Respond to a Touch
How to Downscale a Uiimage in iOS by the Data Size
How to Compare Ssl Certificates Using Afnetworking
Removing from Array During Enumeration in Swift
How to Set a Default Value of a Uipickerview
Differencebetween a Development Provisioning Profile and an Ad Hoc Provisioning Profile
Accessing iOS Safari Web Inspector from Windows MAChine
Flutter iOS Build Failed an Error of Pod Files: Podfile Is Out of Date
Right Aligned Uitextfield Spacebar Does Not Advance Cursor in iOS 7
How to Cast an Nsmutablearray to a Swift Array of a Specific Type
Ios: Determine If Device Language Is Right to Left (Rtl)
iOS Playground Doesn't Show UI Preview
Firebase Dynamic Link Not Opening the App iOS
Swift - Instantiating a Navigation Controller Without Storyboards in App Delegate
Can't Find Pods.Modulemap - Looking in Wrong Directory
Is There Function to Convert Uicolor to Hue Saturation Brightness