A simple and lightweight framework to display looping images using native UIKit and SwiftUI.
Out of the box, Looping supports 3 formats of animated and still images: GIF, APNG and HEIC.
To add WebP support, include the framework LoopingWebP, and enable it once:
import Looping
import LoopingWebP
LoopImage.enableWebP()
- Looping has no external dependency.
- LoopingWebP depends on Google's libwebp.
- CocoaPod support.
- Unit tests coverage.
BackgroundBehavior
(restart, stop, resume).- Progressive decoding option.
- Native support on macOS, watchOS, tvOS.
- iOS 11
- macOS 10.15
- Xcode 11 (swift 5.1)
Swift Package Manager (SwiftPM) is dependency manager as well as a distribution tool.
From Xcode 11 onward, SwiftPM is natively integrated with Xcode. In File -> Swift Packages -> Add Package Dependency
, search for WebPImage's repo URL.
If you're a framework author, you can add following the dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/znly/Looping.git", .from("0.1.0"))
]
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
To integrate Looping into your Xcode project using Carthage, specify it in your Cartfile:
github "znly/Looping" ~> 0.1.0
The prefered method is to use init(data:, scale:)
. Default scale is 1.0.
import Looping
let imageData: Data
let image = try? LoopImage(data: imageData)
Additional convenience initializers are available: init(url:)
and init(named:)
.
Note: If the file name contains a scale factor (eg: @{1,2,3}x) it will be applied to the image.
Rendering happens on a background thread.
import Looping
let image: LoopImage
let imageView = LoopView(loopImage: image)
loopView.contentMode = .scaleAspectFit
loopView.loopMode = .once
import Looping
let image: LoopImage
struct ContentView: View {
var body: some View {
Loop(image)
.loopMode(.once)
.resizable()
.scaledToFit()
}
}
Using either the methods for a single frame LoopImage.cgImage(atFrame:)
and LoopImage.image(atFrame:)
, or for a range of frames LoopImage.cgImages(atRange:)
.
For WebP images, since retrieving the image of at a specific frame requires to go over every intermediary frame to reconstruct the final image, it is recommended to call these methods outside of the main thread.
import Looping
let loopImage: LoopImage
let cgImage: CGImage = loopImage.cgImage()
let image: UIImage = loopImage.image() // alternatively you can use UIImage(loopImage: loopImage)
Looping documentation can be found at znly.github.com/Looping.
Generated using SwiftDoc.
See AUTHORS for the list of contributors.
Assets used in the example app are taken from:
- https://developers.google.com/speed/webp/gallery
- http://littlesvr.ca/apng/gif_apng_webp.html
- http://littlesvr.ca/apng/samples.html
- https://apng.onevcat.com/demo
- https://mathiasbynens.be/demo/animated-webp
- https://github.com/iSparta/iSparta
- https://nokiatech.github.io/heif/comparison.html
The Apache License version 2.0 (Apache2) - see LICENSE for more details.
Copyright © 2020 Zenly [email protected] @zenlyapp