Created
April 30, 2017 10:55
-
-
Save helloworldsmart/e0f68120e3afbf9ae45b8a4da82fdc86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ImageViewController: UIViewController | |
| { | |
| //1. model | |
| // var imageURL: URL? | |
| //5. | |
| var imageURL: URL? { | |
| didSet { | |
| image = nil | |
| //fetchImage() | |
| //9. | |
| if view.window != nil { | |
| fetchImage() | |
| } | |
| } | |
| } | |
| //11. | |
| @IBOutlet weak var scrollView: UIScrollView! { | |
| didSet { | |
| //13. | |
| scrollView.delegate = self | |
| //12. | |
| scrollView.minimumZoomScale = 0.03 | |
| scrollView.maximumZoomScale = 1.0 | |
| scrollView.contentSize = imageView.frame.size | |
| scrollView.addSubview(imageView) | |
| } | |
| } | |
| @IBOutlet weak var spinner: UIActivityIndicatorView! | |
| //6. | |
| private func fetchImage() { | |
| if let url = imageURL { | |
| spinner.startAnimating() | |
| //19 | |
| DispatchQueue.global(qos:.userInitiated).async { [weak self] in | |
| //try try! try? | |
| let urlContents = try? Data(contentsOf: url) | |
| if let imageData = urlContents, url == self?.imageURL { | |
| //20 | |
| DispatchQueue.main.async { | |
| self?.image = UIImage(data: imageData) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| //16註解掉?? | |
| //4. | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| view.addSubview(imageView) | |
| //7. | |
| //imageURL = DemoURL.stanford | |
| } | |
| //8. | |
| override func viewWillAppear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| //10. | |
| if image == nil { | |
| fetchImage() | |
| } | |
| } | |
| //2. | |
| fileprivate var imageView = UIImageView() | |
| //3. | |
| private var image: UIImage? { | |
| get { | |
| return imageView.image | |
| } | |
| set{ | |
| imageView.image = newValue | |
| imageView.sizeToFit() | |
| scrollView?.contentSize = imageView.frame.size | |
| //21 spinner? | |
| spinner?.stopAnimating() | |
| } | |
| } | |
| } | |
| extension ImageViewController: UIScrollViewDelegate { | |
| func viewForZooming(in scrollView: UIScrollView) -> UIView? { | |
| return imageView | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment