2015-10-28 35 views
2

我試圖創建一個UIImage與遠程服務器上承載的Apple的新的分層圖像文件之一。從遠程LSR文件創建UIImage

下面的示例代碼正確下載lsr文件(data var保存一個值),但創建一個新的NSImage會導致一個零值。忽略這個代碼是同步和低效的事實。

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的任何想法?

+0

你發現了一些有趣的東西嗎?我試圖完成同樣的問題,沒有結果... –

回答

2

這就是我如何解決它:

  1. 轉換你.lsr文件到.lcr文件從控制檯這樣做: xcrun --sdk appletvos layerutil --c your_file.lsr
  2. 上傳your_file.lcr服務器
  3. 上把這兩個功能於UTIL類:

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

這將使用圖像但不保存到文檔目錄,如果你需要的解決方案,問我,我會分享。

+1

啊,是的,LCR與LSR。發現這隱藏在Apple的界面準則中:「如果您的應用程序在運行時從內容服務器檢索分層圖像,則必須以運行時分層圖像(.lcr)格式提供這些圖像。」 – ehynds