Search code examples
swiftuiimagetvos

Unable to load LCR image in tvOS apps


I am trying to load LCR image in UIImageView using "contentsOfFile" method as described in apple document but I am getting error with nil image. Can anyone please confirm how we can load LCR images from server?

Code I am using:

UIImage(contentsOfFile: "LCR file url")

Error I am getting in console:

BOMStorage BOMStorageOpenWithSys(const char , Boolean, BomSys ): can't open: '/LCR file url' No such file or directory

One thing I noticed is it adds "/" in front of my actual url. I think its because its looking for files in local storage. Does anyone know whats the solution of it?

I even tried to use this:

Downloaded file—Load images using imageWithContentsOfFile:

but no use :(


Solution

  • 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 :)