Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:55:12+00:00 2026-06-05T08:55:12+00:00

I have a custom UIViewController onto which I’ve added a UIImageView (on the StoryBoard)

  • 0

I have a custom UIViewController onto which I’ve added a UIImageView (on the StoryBoard) and then embedded the UIImageView in a UIScrollView. My program loads an image into the imageView and scales it to properly fit. By proper fit, I mean this:

  • if the aspect ratio of the image exactly matches that of the view, the image will be scaled to exactly fit the view, with no scroll bars
  • otherwise, the image will be scaled to exactly fit in one dimension, with a scrollbar in the other dimension.

I currently do this in viewWillAppear. Below is my code for that method and an auxiliary method that calculates the correct zoomScale:

-(float) findZoomScale {    
    float widthRatio = self.view.bounds.size.width / self.imageView.image.size.width;
    float heightRatio = self.view.bounds.size.height / self.imageView.image.size.height;
    float ratio;
    if (widthRatio > heightRatio) ratio = widthRatio;
    else ratio = heightRatio;
        return ratio;
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.imageView.image = self.image;
    self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
    self.scrollView.contentSize = self.imageView.image.size;
    self.scrollView.zoomScale = [self findZoomScale];
        [self.scrollView flashScrollIndicators];
}

- (void)viewDidLoad {    
    [super viewDidLoad];
    self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.photoURL]];
    self.imageView.image = self.image;
    self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, 
        self.imageView.image.size.height);
    self.scrollView.delegate = self;
    self.scrollView.zoomScale = 1.0;
    self.navigationItem.title = self.photoTitle;
    self.scrollView.autoresizesSubviews = YES;
    self.scrollView.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin | 
        UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | 
        UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | 
        UIViewAutoresizingFlexibleHeight);
}

This code works as I intended for the initial load of an image, whether the iPhone is in landscape or portrait orientation. But when I then rotate the iPhone with the image already loaded, it does not properly rescale. I have tried to do the rescaling in didRotateFromInterfaceOrientation, playing with the scrollView’s zoomScale property, but I can’t get it to work. Is this the correct place to do the rescaling? And if so, how do I do it? Thanks.

Duane

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:55:13+00:00Added an answer on June 5, 2026 at 8:55 am

    I just had this exact problem (I assume you’re on the CS193p assignment 4 as well), and solved it by setting the zoomScale back to 1 before adjusting the UIScrollView’s contentSize upon rotation.

    For example:

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        //reset zoomScale back to 1 so that contentSize can be modified correctly
        self.scrollView.zoomScale = 1;
    
        // reset scrolling area equal to size of image
        self.scrollView.contentSize = self.imageView.image.size;
    
        //reset the image frame to the size of the image
        self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
    
        //set the zoomScale to what we actually want
        self.scrollView.zoomScale = [self findZoomScale];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few custom UITableViewCells - http://img11.imageshack.us/i/customfacilitiescell.png/ which are added to this UIViewController
I have a custom UIViewController subclass controlling a UISCrollView containing an UIImageView, set up
I have a custom UIViewController subclass, which gets pushed on a UINavigationController stack. I
I have a custom UIViewController, which is the only subView of UIView. The UIViewController
I have a custom UIViewController in Interface Builder (in my storyboard) and I want
I have a UITableView , which I am controlling from a custom UIViewController .
I have a storyboard with a UIViewController. Inside this view, I've placed a custom
I have a UIViewController Class which holds 2 custom UIView classes which are: ItemView
I have custom UIViewController which looks like this. @implementation UIViewControllerCustom - (id)init { self
i have a custom UIView which is presented from a UIViewController . This view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.