Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to load gif file from Assets #2139

Open
3 tasks done
leroyli opened this issue Sep 1, 2023 · 1 comment
Open
3 tasks done

How to load gif file from Assets #2139

leroyli opened this issue Sep 1, 2023 · 1 comment

Comments

@leroyli
Copy link

leroyli commented Sep 1, 2023

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

Issue Description

What

I want to use kingfisher load gif from assets, but I can not find any solutions.
[Tell us about the issue]

Reproduce

[The steps to reproduce this issue. What is the url you were trying to load, where did you put your code, etc.]

Other Comment

[Add anything else here]

@japanese-goblinn
Copy link

japanese-goblinn commented Sep 6, 2023

I've ended up doing something like this

import Kingfisher
import UIKit

struct NSDataAssetImageDataProvider: Kingfisher.ImageDataProvider {
    let cacheKey: String
    let dataAsset: NSDataAsset
    
    init?(name: String, bundle: Bundle = .main) {
        guard let dataAsset = NSDataAsset(name: name, bundle: bundle) else { return nil }
        self.dataAsset = dataAsset
        self.cacheKey = "\(bundle.bundlePath)/\(dataAsset.name).\(dataAsset.typeIdentifier)" 
    }
    
    func data(handler: @escaping (Result<Data, Error>) -> Void) {
        handler(.success(dataAsset.data))
    }
}

// somewhere where you want to use it
guard let gifResource = NSDataAssetImageDataProvider(name: "yourDataSetName") else { return }

// I do not cache on disk because reading from assets is faster than reading from disk. Caching to disk will also prevent easy updates of resources because they are cached by path, and there's no way as fas as I know to know if an asset has changed without explicitly changing it's name.
imageView.kf.setImage(
   with: gifResource,
   options: [.diskCacheExpiration(.expired)]
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants