2

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 :(

sanjana
  • 3,034
  • 2
  • 25
  • 31

2 Answers2

3

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

sanjana
  • 3,034
  • 2
  • 25
  • 31
0

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.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Did you read the question? I am asking about LCR images not .PNG :( If I use this code with LCR I get nil image :/ – sanjana Oct 16 '15 at 12:19
  • Yes, I actually did read your question. LCR & PNG images are both handled by UIImage. [If you're looking at the "Incorporating Parallax Images" section of the App Programming Guide for tvOS](https://developer.apple.com/library/prerelease/tvos/documentation/General/Conceptual/AppleTV_PG/CreatingParallaxArtwork.html#//apple_ref/doc/uid/TP40015241-CH19-SW1), it looks like you need to ***download*** the image first. In any event, `contentsOfFile:` is expecting a path to local file, not a URL to a file stored on a remote server. So why not try the technique I suggested up there, using the lcr URL? – Michael Dautermann Oct 16 '15 at 12:29
  • I tried but it gives me nil when I convert data into image :( – sanjana Oct 16 '15 at 12:32
  • if you tried the .lcr file locally to begin with (before putting it up onto the server), would it also be nil? the reason I ask is that I wonder if the .lcr file is even valid. – Michael Dautermann Oct 16 '15 at 12:42
  • If we need to put local apple says we should put .lsr file. But if we need to put on server we should put .lcr file :( Here is more info: https://developer.apple.com/library/prerelease/tvos/documentation/General/Conceptual/AppleTV_PG/CreatingParallaxArtwork.html I converted lsr file into lcr and uploaded to server so it should be valid I think :( – sanjana Oct 16 '15 at 12:45