Unable to load LCR image in tvOS apps

回眸只為那壹抹淺笑 提交于 2019-12-06 10:10:50

Ok I kind of found the work around myself for this problem. So I am posting here if in case anyone else is facing same problem. Below is code with explanation:

if let url = NSURL(string: "https://.lcr-file-url") {
        if let data = NSData(contentsOfURL: url){
          let documents = NSURL(string: NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0])
          let writePath = documents!.URLByAppendingPathComponent("file.lcr")
          data.writeToFile(writePath.absoluteString, atomically: true)
          let image = UIImage(contentsOfFile: writePath.absoluteString)
        }
      }

Basically we need to download the image data and save it in some local file and use contentsOfFile method to have actual image.

If anyone else know better solution than this, I would be happy to hear :)

Michael Dautermann

contentsOfFile assumes you're passing along a path to a local file, not a URL of an image hosted on some remote server.

What you should be doing here is loading data into a NSData object and then passing it to UIImage via UIImage(withData: dataFromServer).

To do it synchronously:

if let url = NSURL(string: "http://www.apple.com/euro/ios/ios8/a/generic/images/og.png") {
    if let data = NSData(contentsOfURL: url){
        let yourImage = UIImage(data: data)
    }
}

The code for which I found in this related question

And here's a blog post on how to do it asynchronously.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!