从以前的ViewController获取网址,然后从网址下载内容

时间:2016-02-26 17:46:47

标签: ios swift

我正在尝试从网址下载图片,网址来自之前的ViewController.Here就是我所做的:

import UIKit

class quotesContentsViewController: UIViewController {

    var feed: quotesContentFeed?

    var urlSession: NSURLSession!

    weak var dataTask: NSURLSessionDataTask?

    var quotesContentURLString: String?

    @IBOutlet var quotes: UILabel!
    @IBOutlet var author: UILabel!

    @IBOutlet var backgroundImage: UIImageView!

    override func viewDidLoad() {

        //let request = NSURLRequest(URL: self.imageURL!)



        super.viewDidLoad()

        if let quotesContentURL = NSURL(string: quotesContentURLString!) {
            self.updateQuotesContentFeed(quotesContentURL, completion: {(feed) -> Void in
                //print(feed!.item.imageURL)*
                self.feed = feed!
                self.author.text = feed!.item.author
                self.quotes.text = feed!.item.quotes

                let imageURLString = self.feed!.item.imageURL
                let imageURL = NSURL(string: imageURLString)
                let request = NSURLRequest(URL: imageURL!)
                    //OK
                self.dataTask = self.urlSession.dataTaskWithRequest(request) {(data, response, error) -> Void in

                        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                            if error == nil && data != nil {
                                print(imageURL)
                                let image = UIImage(data: data!)
                                self.backgroundImage.image = image
                            }
                        })
                    }


            })
        }
        //print(self.feed!.item.imageURL)


        // Do any additional setup after loading the view.
    }

quotesContentURLString是我从之前的ViewController获得的。 updateQuotesContentFeedquotesContentURLfeed是一个包含三个String属性,author,quotes和imageURL的结构。

现在我想从imageURL下载内容,但我所做的并不正常。似乎永远不会执行self.dataTask = self.urlSession.dataTaskWithRequest(request) {(data, response, error) -> Void in

那么如何解决问题呢?

0 个答案:

没有答案