I am trying to load the image from url and show it into collection view cell. I am following MVVM . Here is the URL return single image https://picsum.photos/200 . I have set the cell with delegate and data source , I am not able to find out why the image is not reddening into collection view.
Here is the code for network layer..
import Foundation
import UIKit
enum ImageFetchingError: Error {
case timeout
case unknown
}
protocol CatImageCellModel {
func fetchCatImage(completion: @escaping (Result<UIImage, ImageFetchingError>) -> Void)
}
class NetworkLayer: CatImageCellModel {
func fetchCatImage(completion: @escaping (Result<UIImage, ImageFetchingError>) -> Void) {
let url = "https://picsum.photos/200"
guard let _url = URL(string: url) else {
return
}
URLSession.shared.dataTask(with: _url) { data, response, error in
guard
let data = data,
let image = UIImage(data: data),
let response = response as? HTTPURLResponse,
response.statusCode >= 200 && response.statusCode < 300 else {
completion(.failure(.unknown))
return
}
completion(.success(image))
}.resume()
}
}
Here is the code for view model..
import Foundation
import UIKit
enum ImageViewModelState {
case loading
case loaded(UIImage)
case error
var images: UIImage {
switch self {
case .loaded(let images):
return images
case .loading, .error:
return UIImage()
}
}
}
class ViewModel {
private let networkLayer: NetworkLayer
public var image: UIImage = UIImage()
var updatedState: (() -> Void)?
init(networkLayer: NetworkLayer = NetworkLayer()) {
self.networkLayer = networkLayer
}
var state: ImageViewModelState = .loading {
didSet {
updatedState?()
}
}
func fatchImage() {
networkLayer.fetchCatImage { [weak self] result in
switch result {
case .success(let image):
self?.image = image
self?.state = .loaded(image)
print("Name of the Image: (image)")
case .failure(let error):
print(error.localizedDescription)
self?.state = .error
}
}
}
}
Here is the code for view..
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
private var viewModel = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
viewModel.fatchImage()
collectionView.delegate = self
collectionView.dataSource = self
viewModel.updatedState = { [weak self] in
DispatchQueue.main.async {
self?.collectionView.reloadData()
}
}
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! CollectionViewCell
// return card
let image = viewModel.image
cell.imageView.image = image
return cell
}
}
Here is the cell code ..
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
override func prepareForReuse() {
imageView = nil
imageView?.alpha = 0.5
}
}
Now is saying .. following error .
Here is the result when I run the app which is blank..
screenshot…
(https://i.sstatic.net/v8nKFZIo.png)
14