ImageViewController

public class ImageViewController : UIViewController, UIScrollViewDelegate

An ImageViewController presents an image within a UIScrollView. The controller avoids blocking the UI thread by running the code to get the UIImage to show on the global dispatch queue.

It also is able to handle images from sources other than a URL. It supports images from any source by have the client provide an ImageSource object rather than a URL. That object is responsible for providing an image when asked.

Given an imageViewController, the use case for URL images is the following:

   let url = URL(...)
   let imageSource = ImageSource() {
      if let urlContents = try? Data(contentsOf: url) {
        return UIImage(data: urlContents)
      } else {
        return nil
      }
   }
   imageViewController.imageSource = imageSource

This version also extends the lecture version with:

  • an action for sharing the shown image with other apps. You may connect that handler to a UIBarButton in your UI.

  • a spinng activity indicator that displays when getting the image. You may connect that handler to a UIActivityViewer.

  • If we are loading an image that has not appeared yet, start the spinner in case it is not already spinning.

    Declaration

    Swift

    public override func viewDidAppear(_ animated: Bool)
  • Returns the view to scroll/zoom, ie the image’s view

    Declaration

    Swift

    public func viewForZooming(in scrollView: UIScrollView) -> UIView?