Please note, this blog entry is from a previous course. You might want to check out the current one.
Whenever a photo’s image appears on screen, it should automatically zoom to show as much of the photo as possible with no extra, unused space. Once the user zooms in or out on a photo by pinching, though, you can stop auto-zooming that image.
Instead of setting the zoom scale to a constant value, it needs to be calculated. The scale to fit an image horizontally is calculated by dividing the bounds width of the scroll view by the width of the image. The same would have been true for the vertical scale, using the heights of those values, if the scroll view did not extend over the tab bar, the navigation bar and the status bar. Their heights need to be used to adjust the calculation for the vertical scale. Just set the the zoom scale of the scroll view to the higher of those two calculated scales:
- (void)setImage:(UIImage *)image { ... [self setZoomScaleToFillScreen]; } - (void)setZoomScaleToFillScreen { double wScale = self.scrollView.bounds.size.width / self.imageView.image.size.width; double hScale = (self.scrollView.bounds.size.height - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height - [UIApplication sharedApplication].statusBarFrame.size.height) / self.imageView.image.size.height; if (wScale > hScale) self.scrollView.zoomScale = wScale; else self.scrollView.zoomScale = hScale; }
The complete code is available on github.