2015-02-07 66 views
0

我試圖從URL顯示圖像到imageView。我在一個簡單的項目中使用同步方法成功完成了它。但是這個應用程序用於在線商店,所以我從JSON文件中獲取產品信息和圖像URL。我確信我已成功將所有數據存儲在產品陣列中。但我面臨的問題是:從快速顯示URL中的圖像,展開錯誤

fatal error: unexpectedly found nil while unwrapping an Optional value

這是代碼。

// UnWrapping imageURL 
     let url:NSURL? = NSURL(string: actualCurrentProduct.imageURL) 

     if let actualURL = url { 

      let imageData = NSData(contentsOfURL: actualURL) 

      if let actualImageData = imageData { 

       self.productImageView.image = UIImage(data: actualImageData) 
      } 
     } 

它在此特定代碼中突出顯示。

self.productImageView.image = UIImage(data: actualImageData)

有人知道爲什麼嗎?真的很感激它。

+0

爲me..check烏爾URL工作正常可能是導致這個問題。 – 2015-02-07 12:26:07

+0

也許'self.productImageView'是'nil'? – rintaro 2015-02-07 12:59:53

+0

@DharmeshKheni我檢查了網址,它的工作原理。 – mosesdw 2015-02-07 14:35:31

回答

0

我設法使用此代碼從URL成功顯示圖像。我從零開始開始項目,併爲顯示圖像部分,這是我的代碼。

let imageURL = NSURL(string: actualCurrentProduct.imageURL) 
     if let actualImageURL = imageURL { 

      let imageData = NSData(contentsOfURL: actualImageURL) 
      if let actualImageData = imageData { 

       let image = UIImage(data: actualImageData) 
       if let actualImage = image { 

        self.productImage.image = actualImage 

       } 
      } 
     } 

最後....