Search code examples
objective-cios5uiscrollview

UIScrollView with an embedded UIImageView: Ratio of the contentSize height and width gets changed when zooming in/out


I was writing a simple custom View Controller in ios5 with a UIScrollView and an embedded UIImageView, with the intention of displaying photos and being able to do zooming and panning. These are all embedded in a UINavigationController and TabBarViewController

Here are snippets of my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.delegate = self; 
    self.imageView.image = self.image;
    [self.imageView sizeToFit]; 
    self.scrollView.zoomScale = 1;
    self.scrollView.contentSize = self.imageView.image.size;
    NSLog(@"Original contentSize=(%f,%f), ratio=%f", self.scrollView.contentSize.width, self.scrollView.contentSize.height, self.scrollView.contentSize.height/self.scrollView.contentSize.width);
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return self.imageView;
}

- (void) scrollViewDidZoom:(UIScrollView *)scrollView 
{
    NSLog(@"Changed contentSize=(%f,%f), ratio=%f", self.scrollView.contentSize.width, self.scrollView.contentSize.height, self.scrollView.contentSize.height/self.scrollView.contentSize.width);
}

I've also defaulted the View->Mode in the Attributes inspector to "Top Left" for both the UIScrollView and UIImageView. Zoom min/max has been set to 0.1/10. Delegates and outlets are hooked up.

Now when i run my program, this is what I get when I pinch-zoom on the image:

2012-01-08 19:58:33.529 TestApp[499:f803] Original contentSize=(1024.000000,1024.000000), ratio=1.000000
2012-01-08 19:58:37.599 TestApp[499:f803] Changed contentSize=(1014.381775,922.255310), ratio=0.909180
2012-01-08 19:58:37.600 TestApp[499:f803] Changed contentSize=(1014.381775,922.255310), ratio=0.909180
2012-01-08 19:58:37.730 TestApp[499:f803] Changed contentSize=(1004.763977,913.510986), ratio=0.909180
2012-01-08 19:58:41.432 TestApp[499:f803] Changed contentSize=(974.770325,886.241394), ratio=0.909180
2012-01-08 19:58:41.433 TestApp[499:f803] Changed contentSize=(974.770325,886.241394), ratio=0.909180
2012-01-08 19:58:41.448 TestApp[499:f803] Changed contentSize=(954.777161,868.064026), ratio=0.909180

As you can see, the ratio of the heigh and width of the contentSize changes when I zoom. Does anyone know why this is the case? Am I doing something wrong here? Do I have to manually set the contentSize in scrollViewDidZoom?


Solution

  • As mentioned in my comment, I fixed this by setting the frame.size of the scrollView to (320,367). It was set incorrectly by the storyboard.