Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Load gif from asset catalog #68

Merged
merged 2 commits into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ let jeremyGif = UIImage.gif(name: "jeremy")
// A UIImageView with async loading
let imageView = UIImageView()
imageView.loadGif(name: "jeremy")

// A UIImageView with async loading from asset catalog(from iOS9)
let imageView = UIImageView()
imageView.loadGif(asset: "jeremy")
```

## Installation
Expand Down
21 changes: 21 additions & 0 deletions SwiftGifCommon/UIImage+Gif.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ extension UIImageView {
}
}

@available(iOS 9.0, *)
public func loadGif(asset: String) {
DispatchQueue.global().async {
let image = UIImage.gif(asset: asset)
DispatchQueue.main.async {
self.image = image
}
}
}

}

extension UIImage {
Expand Down Expand Up @@ -67,6 +77,17 @@ extension UIImage {
return gif(data: imageData)
}

@available(iOS 9.0, *)
public class func gif(asset: String) -> UIImage? {
// Create source from assets catalog
guard let dataAsset = NSDataAsset(name: asset) else {
print("SwiftGif: Cannot turn image named \"\(asset)\" into NSDataAsset")
return nil
}

return gif(data: dataAsset.data)
}

internal class func delayForImageAtIndex(_ index: Int, source: CGImageSource!) -> Double {
var delay = 0.1

Expand Down