APNGKit
repository·master·Indexed 25 days ago
https://github.com/onevcat/apngkitA high-performance Swift framework for loading and displaying Animated Portable Network Graphics (APNG) on iOS, macOS, and tvOS. It provides high-level abstractions for animation playback, error handling, and lifecycle monitoring via APNGImage and APNGImageView. The library supports installation via Swift Package Manager and CocoaPods.
What's inside APNGKit
Install Xcode command line tools
masterBefore using fastlane, ensure you have the latest version of the Xcode command line tools installed on your system.
xcode-select --installHandle APNG decoding and playback errors
masterAPNGKit provides mechanisms to handle errors during both image creation and animation playback.
During Image Creation
If
APNGImagefails to initialize, check if the error contains anormalImageto fall back to a static version of the image.During Animation Playback
If specific frames are broken, APNGKit can fall back to the default image. You can monitor these events using:
onDecodingFrameError: Triggered when a frame fails to decode.onFallBackToDefaultImage: Triggered when the view successfully falls back to the default image.onFallBackToDefaultImageFailed: Triggered if the fallback attempt also fails.
// Handling creation errors do { let image = try APNGImage(named: "image_name") imageView.image = image } catch { if let normalImage = error.apngError?.normalImage { imageView.staticImage = normalImage } else { print("Error: \(error)") } } // Monitoring playback errors imageView.onDecodingFrameError.delegate(on: self) { (self, error) in print("A frame cannot be decoded.") } imageView.onFallBackToDefaultImage.delegate(on: self) { (self, _) in print("Fall back to default image.") }Install APNGKit via CocoaPods
masterTo integrate APNGKit using CocoaPods, add it to your
Podfileas shown below. Ensure you useuse_frameworks!and specify the version requirement.source 'https://github.com/CocoaPods/Specs.git' platform :ios, '9.0' use_frameworks! target 'your_app' do pod 'APNGKit', '~> 2.0' endInstall APNGKit via Swift Package Manager
masterThe recommended way to install APNGKit is using Swift Package Manager. In Xcode, follow these steps:
- Go to File > Swift Packages > Add Package Dependency.
- Enter the repository URL:
https://github.com/onevcat/APNGKit.git. - Select the dependency rule Up to Next Major with version 2.0.0.
Prevent Xcode from compressing APNG files
masterXcode's default build process compresses PNG files, which can strip the APNG animation data and turn it into a static image. If you see the error
CgBI chunk found. It seems that the input image is compressed by Xcode..., you have two options:- Rename the file extension: Change your
.pngfiles to.apng. APNGKit will detect and handle this extension seamlessly, and Xcode will not attempt to compress them. - Disable PNG compression: Set
COMPRESS_PNG_FILEStoNOin your app target's build settings. Note that this affects all PNGs in your project.
- Rename the file extension: Change your
Control APNG animation playback
masterAnimations play automatically by default when an image is assigned to an
APNGImageView. To control this behavior, use theautoStartAnimationWhenSetImageproperty and thestartAnimating()method.let imageView = APNGImageView(frame: .zero) // Disable automatic playback imageView.autoStartAnimationWhenSetImage = false imageView.image = image // Start the animation manually imageView.startAnimating()Display an APNG Image with APNGImageView
masterTo display an animation, use
APNGImageView, which is a subclass ofUIView(iOS) orNSView(macOS).Note for Interface Builder users: Drag a standard
UIVieworNSViewto your canvas (do NOT useUIImageVieworNSImageView) and change its class toAPNGImageViewin the Identity Inspector.let image: APNGImage = ... // Initialize and add the view let imageView = APNGImageView(image: image) view.addSubview(imageView)Load an APNG Image
masterUse the
APNGImageinitializer to load images from various sources. Note that these initializers are throwable and can return anAPNGKitError.decoderErrorif the image is invalid.import APNGKit // Load an APNG image from file in main bundle var image = try APNGImage(named: "your_image") // Load an APNG image from a specific file URL if let url = Bundle.main.url(forResource: "your_image", withExtension: "apng") { image = try APNGImage(fileURL: url) } // Load an APNG image from raw Data let data: Data = ... image = try APNGImage(data: data)Handle APNG playback lifecycle with delegates
masterAPNGKit provides delegates to monitor the animation lifecycle, such as when a single loop finishes or when the entire animation is complete.
onOnePlayDone: Triggered after each loop (respectingnumberOfPlaysinAPNGImage).onAllPlaysDone: Triggered when the entire animation sequence is finished.onFramesInformationPrepared: Triggered when all frame information is loaded, allowing you to access the fullimage.duration.
// Monitor single loop completion imageView.onOnePlayDone.delegate(on: self) { (self, count) in print("Played: \(count)") } // Monitor total animation completion imageView.onAllPlaysDone.delegate(on: self) { (self, _) in print("All done.") } // Get full duration once frames are prepared let image = try APNGImage(named: "image") image.onFramesInformationPrepared.delegate(on: self) { (self, _) in switch image.duration { case .full(let duration): print("Full duration: \(duration)") case .partial: print("This should not happen.") } }Run iOS fastlane actions
masterThe following fastlane actions are available for iOS development. You can run them using
fastlane <action>orbundle exec fastlane <action>if you are using Bundler.### ios test ```sh [bundle exec] fastlane ios testios test_macOS
[bundle exec] fastlane ios test_macOSios test_iOS
[bundle exec] fastlane ios test_iOSios test_tvOS
[bundle exec] fastlane ios test_tvOSios release
[bundle exec] fastlane ios releaseios change_log
[bundle exec] fastlane ios change_logios lint
[bundle exec] fastlane ios lint