I'm displaying 2 images in a collectionview using 2 URL's like so...
var sampleURLArray: [NSURL] = []
sampleURLArray = [NSURL(string: "http://myApp.abc.com/products/123_45_6_image_123456")!,NSURL(string: "http://myApp.abc.com/products/789_45_6_image_654321")!]
And I'm iterating through the URL's and assigning the images to an array like so...
for imageNumber in self.sampleURLArray {
let imageurl1 = imageNumber
let url = imageurl1
let data = try? Data(contentsOf: url as URL)
self.sampleImageView.image = UIImage(data: data!)
self.arrayOfURLImages.append(self.sampleImageView.image!)
}
The images are now properly shown. But I am not to display images in such a hard-coded fashion and I have to display the images from the url's I'm getting from my json response which I'm doing like so...
if let projectData = result["Productdata"] as? [[String:Any]] {
let productIds = projectData.flatMap({ $0["product_id"] as? String })
let images = projectData.flatMap({ $0["product_images"] as? [[String: Any]] }).flatMap({ $0 })
let imageIds = images.flatMap({ $0["image"] as? String })
self.appDelegate.commonArrForCollectionViewImgs = imageIds //I'll get each URL from self.appDelegate.commonArrForCollectionViewImgs
}
//CommonArrForCollectionViewImgs is set as var commonArrForCollectionViewImgs = [String]()
for imageNumber in self.appDelegate.commonArrForCollectionViewImgs {
let imageurl1 = imageNumber
let url = URL(string: imageurl1)
print(url)
if let dataOfUrl = try? Data(contentsOf: url!) {
let data = dataOfUrl
self.sampleImageView.image = UIImage(data: data)
}
self.arrayOfURLImages.append(self.sampleImageView.image!)
}
But when this is done, the images are not shown in the collectionview though I am able to parse all url's properly. Also, I'm doing the json parsing if I'm getting success in the API request. I'm not able to figure out what the issue is. Does this have to do anything with where the collectionview is set up...?