0

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?'

David Seek
  • 16,783
  • 19
  • 105
  • 136

2 Answers2

2

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];
sonifex
  • 280
  • 1
  • 2
  • 14
  • is it possible to find the pictures using css/xpath using this library? – David Seek Feb 25 '16 at 16:31
  • No, You need to get image url using different library. I think you have already got the url from css/xpath. Just use sdwebImage for downloading image from a url . – sonifex Feb 26 '16 at 12:46
1

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!)
Community
  • 1
  • 1
Russell
  • 5,436
  • 2
  • 19
  • 27
  • there's a good solution, but it doesn't really fit my needs since I have to get the images using xpath. in the solution I'm only able to find them hard coded – David Seek Feb 25 '16 at 16:32
  • ok - so it's a two-part question? how to find all of the images and also how to display them? – Russell Feb 25 '16 at 16:39
  • yes. I know I can find them using xpath-contains:"xy". I'm familiar with that in java. but yes. I need to display them into an collectionview. – David Seek Feb 25 '16 at 17:25