David Seek
David Seek

Reputation: 17132

Swift / iOS / Kanna / parse image from html using xpath

I'm using the Kanna framework to use xpath parsing. I'm successfully getting text from my desired url using the following code:

    @IBAction func getSource(sender: AnyObject) {

    if let doc = Kanna.HTML(url: (NSURL(string: "http://qwertz.com")!), encoding: NSUTF8StringEncoding) {


        // Search for nodes by XPath
        for link in doc.xpath(".//*[@id='bday_form']/div[1]/img[1]") {
            print(link.text)
            print(link["href"])

            self.testLabel.text = link.text
        }
    }

    print("test22")

}

How can I parse images and put them into my imageView Outlet @IBOutlet weak var imageViewOutlet: UIImageView!

Help is very appreciated.

self.imageViewOutlet.image = link

throws: Cannot assign value of type 'XMLElement' to type 'UIImage?'

Upvotes: 0

Views: 1678

Answers (2)

sonifex
sonifex

Reputation: 280

You have to download the image before assign to UIImageView. There is really good library to download image from the web. It has swift support also.

Here is :https://github.com/rs/SDWebImage

Here is an example:

NSURL *url = [NSURL URLWithString:link]
[self.imageView sd_setImageWithURL:url];

Upvotes: 2

Russell
Russell

Reputation: 5554

you need to download the image from the url first, and then create the UIImage

Here's an example of how to do this Loading/Downloading image from URL on Swift where the shortest approach (from @Lucas Eduardo) looks like this

let url = NSURL(string: image.url)
let data = NSData(contentsOfURL: url!)
imageURL.image = UIImage(data: data!)

Upvotes: 1

Related Questions