I am trying to zoom into a UIImage so I can see as much of the photo as possible without showing white space. Thus in portrait I want to zoom so that the shorter of the two sides of the image fits fully in the screen. All of the following code is in viewDidLoad.
I have the UIImageView and UIScrollView set up as follows:
self.scrollView.delegate = self;
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
At this point zooming and scrolling within the UIScrollView works perfectly.
Then I implement an if-else so that when the picture is taller than it is wide I call:
[self.scrollview zoomToRect:CGRectMake(0, 0, IMAGE_WIDTH, 1)];
This works perfectly. The width of the image fills the width of the screen and I can only scroll up and down.
But when I call the exact same function for the height:
[self.scrollview zoomToRect:CGRectMake(0, 0, 1, IMAGE_HEIGHT)];
Nothing! No zoom at all as if the function was never called. But I set a breakpoint and stepped through the code in the debugger and everything was called perfectly, even the CGRectMake function itself. What’s going on?
As per the comments above, make sure you select a rect with both a width and a height for the given aspect ratio you want to zoom to.