2

I have been using Nuke up until last week but I switched to SDWebImage because it is a few times more popular than Nuke and seems more built out.

However, after switching to SDWebImage I have noticed significantly longer build times. I am not asking for an opinion here. In terms of tangible metrics, is Nuke more performant and lighter than SDWebImage?

Although I do not know SDWebImage inside and out, it seems SDWebImage has tons of feature but lacks a key one: I have not found how to set a failure image in case of network failure while loading image. Although, it is extremely easy to do in Nuke.

    let options = ImageLoadingOptions(
        failureImage: #imageLiteral(resourceName: "image-not-found")
    )
    Nuke.loadImage(with: URL(string: products[indexPath.row].imageLink)!, options: options, into: cell.productImage)
Rage
  • 870
  • 9
  • 27

2 Answers2

0

Can you use placeholderImage for failure image?

imageView.sd_setImage(
  with: url,
  placeholderImage: #imageLiteral(resourceName: "placeholder")
)
Hlung
  • 13,850
  • 6
  • 71
  • 90
-5

It's not neccesary/recommended to use external frameworks just for loading an image from an URL.

If you use an external framework you are adding unnecesary codes to your project because there is no framework that just add a function to load an image, it's probably that you are adding more unnecesary stuff with that framework.

I recommend you to implement it by yourself! It going to be more performant and easy than adding an external framework.

This is a code to load an image without external frameworks:

import UIKit

typealias GMImageServiceSuccess = (UIImage) -> Void
typealias GMImageServiceFail = (Error) -> Void

class GMImageService {
    // MARK: - Class vars.
    private static let imageServiceCache = NSCache<NSString, UIImage>()
    
    // MARK: - Vars.
    private var currentDataTask: URLSessionDataTask?
        
    // MARK: - Fetch images functions.
    func gmImageFromURL(_ urlString: String, sucess: GMImageServiceSuccess?, fail: GMImageServiceFail?) {
        if let imageFromCache = GMImageService.imageServiceCache.object(forKey: urlString as NSString) {
            sucess?(imageFromCache)
            return
        }
        
        guard let imageURL = URL(string: urlString) else {
            // ERROR.
            return
        }
        
        URLSession.shared.dataTask(with: imageURL) { data, response, error in
            guard let imageData = data else {
                // ERROR.
                return
            }
            
            if let imageToCache = UIImage(data: imageData) {
                DispatchQueue.main.async {
                    GMImageService.imageServiceCache.setObject(imageToCache, forKey: urlString as NSString)
                    sucess?(imageToCache)
                }
            } else {
                // ERROR.
            }
        }.resume()
    }
    
    // MARK: - Cancel images functions.
    func mpImageCancelCurrent() {
        if let currentTask = self.currentDataTask {
            currentTask.cancel()
        }
    }
}

Let me know if you need to implement an image cache, it's easy in swift!

Gastón Antonio Montes
  • 2,559
  • 2
  • 12
  • 15
  • Sorry for the misunderstanding but I am using SDWebImage for image caching. – Rage Jul 14 '20 at 22:18
  • Implementing an image cache when you are loading an image from an URL it's an easy issue, I recommend you not to use this kind of frameworks for this issue. I'll edit my comment with an image cache implemented. – Gastón Antonio Montes Jul 15 '20 at 22:18
  • Take a look to my code, it's loading an image from url and saving it to the cache if the image is not already in the cache. I'm not using unneccesary, heavy and slow frameworks like fisheye, SDWebImage and Nuke. – Gastón Antonio Montes Jul 15 '20 at 22:31
  • Thank you, I may use this. However, the frameworks are slow to build when running on the simulator but have the same speed as manual implementation. – Rage Jul 16 '20 at 00:05
  • Don’t reinvent the wheel. – AaoIi Jul 18 '20 at 15:53
  • Load image asynchronously and then update UI is in fact is complicated process, and there are many unexpected edge cases can happen. For instance, if your `UICollectionView` is displaying tone of images and you want to have a smooth scrolling experience, those frameworks are very handy. You really don't want with those, especially thing will become complicated when cell reusing kick in. There are reasons why those frameworks are popular, as they are solving difficult real problem. – Cheok Yan Cheng Aug 22 '21 at 19:44