无法在 tvOS 应用程序中加载 LCR 图像

Unable to load LCR image in tvOS apps

我正在尝试使用 "contentsOfFile" 方法在 UIImageView 中加载 LCR 图像,如 apple 文档中所述,但我收到 nil 图像错误。谁能确认我们如何从服务器加载 LCR 图像?

我使用的代码:

UIImage(contentsOfFile: "LCR file url")

我进入控制台时出错:

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

我注意到的一件事是它在我的 url 前面添加了“/”。我认为这是因为它在本地存储中寻找文件。有谁知道解决方法是什么?

我什至试过用这个:

Downloaded file—Load images using imageWithContentsOfFile:

但没用:(

contentsOfFile 假定您传递的是本地文件的路径,而不是某个远程服务器上托管的图像的 URL。

您在这里应该做的是将数据加载到 NSData 对象中,然后通过 UIImage(withData: dataFromServer).

将其传递给 UIImage

同步执行:

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

我找到的代码 in this related question

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

好的,我自己找到了解决这个问题的方法。所以如果其他人遇到同样的问题,我会在这里发帖。下面是带有解释的代码:

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

基本上我们需要下载图像数据并将其保存在一些本地文件中,然后使用 contentsOfFile 方法来获得实际图像。

如果有人知道比这更好的解决方案,我会很高兴听到:)