Giter VIP home page Giter VIP logo

Comments (4)

lsmilek1 avatar lsmilek1 commented on July 17, 2024

Hi, for me worked following:

`class ImageDownloader {

private var imageDataTask: URLSessionDataTask?
private let cache = ImageCache.cache

private(set) var isCancelled = false

func downloadPhoto(with url: URL, completion: @escaping ((UIImage?, Bool) -> Void)) {
    guard imageDataTask == nil else { return }

    isCancelled = false

    if let cachedResponse = cache.cachedResponse(for: URLRequest(url: url)),
        let image = UIImage(data: cachedResponse.data) {
        completion(image, true)
        return
    }

    let config = URLSessionConfiguration.default
    config.urlCache = cache
    let session = URLSession(configuration: config)

    imageDataTask = session.dataTask(with: url) { [weak self] (data, response, error) in
        guard let strongSelf = self else { return }
        strongSelf.imageDataTask = nil

        guard let data = data, let response = response, let image = UIImage(data: data), error == nil else { return }

        let cachedResponse = CachedURLResponse(response: response, data: data)
        strongSelf.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url))

        DispatchQueue.main.async {
            completion(image, false)
        }
    }

    imageDataTask?.resume()
}

func cancel() {
    isCancelled = true
    imageDataTask?.cancel()
}

}
`

this bit is important:

`let config = URLSessionConfiguration.default
config.urlCache = cache
let session = URLSession(configuration: config)

    imageDataTask = session.dataTask(with: url) { [weak self]...`

the cache has to be asigned to the session.

hope it helps

from unsplash-photopicker-ios.

Kondamon avatar Kondamon commented on July 17, 2024

There is an open pull request #34 related to this issue. @ocollet, what needs to be done to fix the caching issue?

from unsplash-photopicker-ios.

Kondamon avatar Kondamon commented on July 17, 2024
    ...
    let config = URLSessionConfiguration.default
    config.urlCache = cache
    let session = URLSession(configuration: config)
    imageDataTask = session.dataTask(with: url) { [weak self] (data, response, error) in
    ...

This didn't work for me

from unsplash-photopicker-ios.

ocollet avatar ocollet commented on July 17, 2024

Fixed with pull-request #34

from unsplash-photopicker-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.