Advertisement
Guest User

Untitled

a guest
Jun 29th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ImageViewController.swift
  3. //  TableView
  4. //
  5. //  Created by Neil Quinn on 23/06/2014.
  6. //  Copyright (c) 2014 Neil Quinn. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ImageViewController: UIViewController, UIScrollViewDelegate {
  12.  
  13.     var imageURL : NSURL = NSURL() {
  14.         didSet {
  15.             self.startDownloadingImage()
  16.         }
  17.     }
  18.    
  19.     @lazy var imageView = UIImageView()
  20.     @IBOutlet var spinner : UIActivityIndicatorView
  21.     @IBOutlet var scrollView : UIScrollView
  22.    
  23.     var image : UIImage? {
  24.         set{
  25.             self.scrollView.zoomScale = 1.0
  26.             self.scrollView.contentSize = self.image ? self.image!.size : CGSizeZero
  27.             self.spinner.stopAnimating()
  28.             self.imageView.frame = CGRectMake(0, 0, image!.size.width, image!.size.height)
  29.             self.imageView.image = image
  30.         }
  31.    
  32.         get{
  33.             return self.imageView.image
  34.         }
  35.     }
  36.  
  37.     override func viewDidLoad() {
  38.         super.viewDidLoad()
  39.  
  40.         // Do any additional setup after loading the view.
  41.         self.scrollView.addSubview(self.imageView)
  42.         self.scrollView.minimumZoomScale = 0.2
  43.         self.scrollView.maximumZoomScale = 2.0
  44.         self.scrollView.delegate = self;
  45.     }
  46.  
  47.     func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView
  48.     {
  49.         return self.imageView
  50.     }
  51.    
  52.     func startDownloadingImage()
  53.     {
  54.         self.image = nil
  55.        
  56.         if self.imageURL != nil {
  57.         //   self.spinner.startAnimating()
  58.             var request : NSURLRequest = NSURLRequest(URL: self.imageURL)
  59.            
  60.             //another configuration option is the backgroundSessionConfiguration (multitasking API required though)
  61.             var configuration : NSURLSessionConfiguration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
  62.             // create the session without specifying a queue to run completion handler on (thus not main queue
  63.             // we also don't specify a delegate (since completion handler is all we need)
  64.             var session : NSURLSession = NSURLSession(configuration: configuration)
  65.             var task = session.dataTaskWithRequest(request, completionHandler: { localdatafile, response, error in
  66.                 if !error {
  67.                     if request.URL.isEqual(self.imageURL) {
  68.                         var localString : NSString = NSString(data: localdatafile, encoding:NSUTF8StringEncoding)
  69.                         var localfile : NSURL = NSURL(string: localString)
  70.                        
  71.                         var image : UIImage = UIImage(data: NSData(contentsOfURL: localfile))
  72.                         dispatch_async(dispatch_get_main_queue(), {self.image = image})
  73.                     }
  74.                 }
  75.             })
  76.            
  77.             task.resume()
  78.        
  79.         }
  80.     }
  81.    
  82.    
  83.     override func didReceiveMemoryWarning() {
  84.         super.didReceiveMemoryWarning()
  85.         // Dispose of any resources that can be recreated.
  86.     }
  87.    
  88.  
  89.     /*
  90.     // #pragma mark - Navigation
  91.  
  92.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  93.     override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
  94.         // Get the new view controller using [segue destinationViewController].
  95.         // Pass the selected object to the new view controller.
  96.     }
  97.     */
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement