从远程 LSR 文件创建 UIImage

Creating a UIImage from a remote LSR file

我正在尝试使用托管在远程服务器上的 Apple 新分层图像文件之一创建 UIImage。

下面的示例代码正确下载了 lsr 文件(data var 保存了一个值),但是用它创建一个新的 NSImage 会导致一个 nil 值。忽略这段代码是同步和低效的事实。

if let url = NSURL(string: "http://path/to/my/layered/image.lsr") {
    if let data = NSData(contentsOfURL: url) {
        let image = UIImage(data: data) // `image` var is nil here
        imageView?.image = image
    }
}

关于如何下载 LSR 并用它创建 UIImage 有什么想法吗?

我就是这样解决的:

  1. 从控制台将 .lsr 文件转换为 .lcr 文件: xcrun --sdk appletvos layerutil --c your_file.lsr
  2. 上传your_file.lcr到您的服务器
  3. 将这两个函数放到一个util中class:

    func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void)) {
        NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
            completion(data: data, response: response, error: error)
        }.resume()
    }
    
    func downloadImage(url: NSURL, imageView: UIImageView){
        print("Started downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".")
        getDataFromUrl(url) { (data, response, error)  in
            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                guard let data = data where error == nil else { return }
                print("Finished downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".")
                imageView.image = UIImage(data: data)
            }
        }
    }
    
  4. 这样使用:

    if let checkedUrl = NSURL(string: "http://domain/path/to/your_file.lcr") {
        self.my_ui_view.contentMode = .ScaleAspectFit
        downloadImage(checkedUrl, imageView: self.my_ui_view.contentMode)
    }
    

这将使用图像而不将其保存到文档目录,如果您需要该解决方案,请问我,我会分享。