Swift
Swift

Reputation: 1184

Why getting nil in collectionView image from json in swift

I have collectionview with image and label... I'm able to display text values from json to label and I'm getting all img urls from json to cellForItemAtindexPath but all those images i am unable to show in collectionview.. i have all 20 image urls in cellForItemAtindexPath i can see it in console but why i am unable to display them in collectionview.

here is my code:

 import UIKit
 import SDWebImage
 struct JsonData {

var iconHome: String?
var typeName: String?
var id: String?
init(icon: String, tpe: String, id: String) {
    self.iconHome = icon
    self.typeName = tpe
    self.id = id
}
}

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate, URLSessionDelegate {

@IBOutlet weak var collectionView: UICollectionView!
var itemsArray = [JsonData]()
override func viewDidLoad() {
    super.viewDidLoad()
    homeServiceCall()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return itemsArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell

    let aData = itemsArray[indexPath.row]
    cell.paymentLabel.text = aData.typeName
    cell.paymentImage.sd_setImage(with: URL(string:aData.iconHome!)) { (_, error, _, _) in
        if let error = error {
            print(error)
        }
    }
    print("tableview collection images \(String(describing: aData.iconHome))")
   return cell
}
//MARK:- Service-call

func homeServiceCall(){

    let urlStr = "https://********/webservices//getfinancer"
    let url = URL(string: urlStr)
    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

        guard let respData = data else {
            return
        }
        guard error == nil else {
            print("error")
            return
        }
        do{
            let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
            let financerArray = jsonObj["financer"] as! [[String: Any]]

             for financer in financerArray {
             guard let id = financer["id"] as? String else { break }
             guard let pic = financer["icon"] as? String else { break }
             guard let typeName = financer["tpe"] as? String else { break }
             print("the json icons \(String(describing: pic))")

             let jsonDataObj = JsonData(icon: pic, tpe: typeName, id: id)
             self.itemsArray.append(jsonDataObj)
             }
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
        catch {
            print("catch error")
        }
    }).resume()
}
}

when i print image urls in cellForItemAtindexPath i got all 20 image urls in console.. but why i am unable to show them in collectionview.

i am getting output like below.. some images are showing and some are not if i give placeholder image in sd_setImage then it shoes placeholder image in collectionview why??

here is my output:

enter image description here

some images are coming and some are not but there are no nil images in server all images are coming in json.. i got stuck here from long time.. anyone please help me here.

Upvotes: 0

Views: 261

Answers (1)

jacob
jacob

Reputation: 1052

Because you are using Swift so i recommend that you should use KingFisher instead of SDWebImage to handle images from urls.

I checked your code, everything is fine. However, when you load image from url, some of them throw this error:

A URL session error happened. The underlying error: Error Domain=NSURLErrorDomain Code=-1202 \"The certificate for this server is invalid. You might be connecting to a server that is pretending to be “anyemi.com” which could put your confidential information at risk."

This error happens for urls with domain anyemi.com. For example: https://anyemi.com/PaySTAR/images/LSPUBLICSCHOOL_icon.png https://anyemi.com/PaySTAR/images/CDMA_icon.png

Urls with domain dev.anyemi.com work well. For example: https://dev.anyemi.com/maheshbank/icons/electricity.png https://dev.anyemi.com/maheshbank/icons/gas.png

Therefore the problem is in SSL configuration of your backend. For now, you can change the url with domain anyemi.com to dev.anyemi.com for testing and i believe that it will work well.

Upvotes: 1

Related Questions