Skip to content

Instantly share code, notes, and snippets.

@helloworldsmart
Created April 23, 2017 11:29
Show Gist options
  • Select an option

  • Save helloworldsmart/07719af91377cd86e1dbc17173bc028d to your computer and use it in GitHub Desktop.

Select an option

Save helloworldsmart/07719af91377cd86e1dbc17173bc028d to your computer and use it in GitHub Desktop.
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)
}
}
//6.
private func fetchImage() {
if let url = imageURL {
//try try! try?
let urlContents = try? Data(contentsOf: url)
if let imageData = urlContents {
image = UIImage(data: imageData)
}
}
}
//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
}
}
}
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