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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…