Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ImageViewController.swift
- // TableView
- //
- // Created by Neil Quinn on 23/06/2014.
- // Copyright (c) 2014 Neil Quinn. All rights reserved.
- //
- import UIKit
- class ImageViewController: UIViewController, UIScrollViewDelegate {
- var imageURL : NSURL = NSURL() {
- didSet {
- self.startDownloadingImage()
- }
- }
- @lazy var imageView = UIImageView()
- @IBOutlet var spinner : UIActivityIndicatorView
- @IBOutlet var scrollView : UIScrollView
- var image : UIImage? {
- set{
- self.scrollView.zoomScale = 1.0
- self.scrollView.contentSize = self.image ? self.image!.size : CGSizeZero
- self.spinner.stopAnimating()
- self.imageView.frame = CGRectMake(0, 0, image!.size.width, image!.size.height)
- self.imageView.image = image
- }
- get{
- return self.imageView.image
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- self.scrollView.addSubview(self.imageView)
- self.scrollView.minimumZoomScale = 0.2
- self.scrollView.maximumZoomScale = 2.0
- self.scrollView.delegate = self;
- }
- func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView
- {
- return self.imageView
- }
- func startDownloadingImage()
- {
- self.image = nil
- if self.imageURL != nil {
- // self.spinner.startAnimating()
- var request : NSURLRequest = NSURLRequest(URL: self.imageURL)
- //another configuration option is the backgroundSessionConfiguration (multitasking API required though)
- var configuration : NSURLSessionConfiguration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
- // create the session without specifying a queue to run completion handler on (thus not main queue
- // we also don't specify a delegate (since completion handler is all we need)
- var session : NSURLSession = NSURLSession(configuration: configuration)
- var task = session.dataTaskWithRequest(request, completionHandler: { localdatafile, response, error in
- if !error {
- if request.URL.isEqual(self.imageURL) {
- var localString : NSString = NSString(data: localdatafile, encoding:NSUTF8StringEncoding)
- var localfile : NSURL = NSURL(string: localString)
- var image : UIImage = UIImage(data: NSData(contentsOfURL: localfile))
- dispatch_async(dispatch_get_main_queue(), {self.image = image})
- }
- }
- })
- task.resume()
- }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- /*
- // #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement