Search code examples
iosswiftwatchkitapple-watchwkinterfaceimage

image by url for Apple Watch


I am trying to use this solution: load-image-from-url-on-watchkit

I have a table on the watch that I am trying to load images for in each row. I am running this code and then I update the text fields of the table. Without trying to get the images, all the text loads in correctly.

But when I add this code:

    let site = "myurl.com/image.jpeg"    //I have copied this and it is an image
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                let url:NSURL = NSURL(string:site)!
                var data:NSData = NSData(contentsOfURL: url)!
                var placeholder = UIImage(data: data)!

                dispatch_async(dispatch_get_main_queue()) {
                    row.myRowImage.setImage(placeholder)
                }
            }

I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value

And I cannot seem to figure out what would cause this if it is a valid url and other parts of the table have loaded correctly. Any help is greatly appreciated!


Solution

  • The URL string need to conform to URL format as described in RFC 2396 . Basically, you need to have http:// or ftp, etc. at the beginning of your URL string.