我有一個字符串,我用逗號分隔的圖像鏈接。下面是我如何將它分解成一個數組:let imagesLinks = imageLins.components(separatedBy: ",")
。然後我用for循環得到一個鏈接,下載的圖片,並將其在UIImage
陣列以這種方式儲存:SDWebImage給零無網址
for imag in imagesLinks
{
let img = UIImageView()
print("\(baseReportImageURL)\(imag)")
img.sd_setImage(with: URL(string: "\(baseReportImageURL)\(imag)"), placeholderImage: nil)
imagesArray.append(img.image!)
}
print語句是給我的,當我在瀏覽器中打開正確的URL下載圖片。問題出在我追加陣列的行上,即imagesArray.append(img.image!)
。我得到:
fatal error: unexpectedly found nil while unwrapping an Optional value
和
fatal error: unexpectedly found nil while unwrapping an Optional value
那麼,什麼將是這個正確的解決方案?
UPDATE
我的問題是不同的,因爲我使用SDWebImage
,當我使用完畢塊有應用程序的一個奇怪的行爲:
img.sd_setImage(with: imgURL, placeholderImage: nil,options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
activityIndicator.stopAnimating()
imagesArray.append(image!)
self.photoCollection.reloadData()
})
所以不斷旋轉的活動指示燈,當我返回並再次推送視圖時,它立即加載圖像。所以我認爲在下載圖像時不會調用完成塊,但爲什麼?
的可能的複製[檢查URL圖像存在(https://stackoverflow.com/questions/29962938/check-if -url-image-exist) – Desdenova
確保你的'imageLinks'沒有空鏈接。 –
@Chaudhry Talha更新後獲取圖像數組? –