2015-02-07 56 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 

       } 
      } 
     } 

最后....