GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added support to encode APNG instead of GIF, when no animated image format provided by dreampiggy · Pull Request #3867 · SDWebImage/SDWebImage

Added support to encode APNG instead of GIF, when no animated image format provided #3867

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.

Sign up for GitHub

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

Merged
dreampiggy merged 3 commits into SDWebImage:master from dreampiggy:feature/encode_format_unknown_animation
Feb 6, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Added support to encode APNG instead of GIF, when no animated image format provided #3867

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
6348850
Added support to encode APNG instead of GIF, when no animated image f…
dreampiggy Feb 6, 2026
a926694
Update the animatedImageWithFrames on macOS as well
dreampiggy Feb 6, 2026
77f2d03
Fix the unit test related to changes
dreampiggy Feb 6, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

6 changes: 5 additions & 1 deletion 6 SDWebImage/Core/SDImageCache.m
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ - (void)storeImage:(nullable UIImage *)image
if (format == SDImageFormatUndefined) {
// If image is animated, use GIF (APNG may be better, but has bugs before macOS 10.14)
if (image.sd_imageFrameCount > 1) {
format = SDImageFormatGIF;
if (@available(iOS 12.0, tvOS 12.0, macOS 10.14, watchOS 5.0, *)) {
format = SDImageFormatPNG;
} else {
format = SDImageFormatGIF;
}
Comment thread
Comment on lines 276 to +283

@coderabbitai coderabbitai Bot Feb 6, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Stale comment on Line 277.

The comment still reads "use GIF (APNG may be better, but has bugs before macOS 10.14)" but the code now conditionally uses PNG/APNG on newer OS versions. Update the comment to reflect the new behavior.

📝 Suggested comment update
-                // If image is animated, use GIF (APNG may be better, but has bugs before macOS 10.14)
+                // If image is animated, prefer APNG on newer OS versions for better quality; fall back to GIF on older OS versions (APNG has bugs before macOS 10.14)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (format == SDImageFormatUndefined) {
// If image is animated, use GIF (APNG may be better, but has bugs before macOS 10.14)
if (image.sd_imageFrameCount > 1) {
format = SDImageFormatGIF;
if (@available(iOS 12.0, tvOS 12.0, macOS 10.14, watchOS 5.0, *)) {
format = SDImageFormatPNG;
} else {
format = SDImageFormatGIF;
}
if (format == SDImageFormatUndefined) {
// If image is animated, prefer APNG on newer OS versions for better quality; fall back to GIF on older OS versions (APNG has bugs before macOS 10.14)
if (image.sd_imageFrameCount > 1) {
if (`@available`(iOS 12.0, tvOS 12.0, macOS 10.14, watchOS 5.0, *)) {
format = SDImageFormatPNG;
} else {
format = SDImageFormatGIF;
}
🤖 Prompt for AI Agents
In `@SDWebImage/Core/SDImageCache.m` around lines 276 - 283, Update the stale
comment near the SDImageFormatUndefined check to reflect the conditional choice
of PNG/APNG on newer OSes: mention that if image.sd_imageFrameCount > 1 the code
uses PNG (APNG on supported OS versions) when available and falls back to GIF on
older OSes (SDImageFormatPNG vs SDImageFormatGIF), so modify the comment to
describe that behavior instead of saying "use GIF (APNG may be better...)".

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
} else {
// If we do not have any data to detect image format, check whether it contains alpha channel to use PNG or JPEG format
format = [SDImageCoderHelper CGImageContainsAlpha:image.CGImage] ? SDImageFormatPNG : SDImageFormatJPEG;
Expand Down
3 changes: 2 additions & 1 deletion 3 SDWebImage/Core/SDImageCoderHelper.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ typedef struct SDImagePixelFormat {
/**
Return an animated image with frames array.
For UIKit, this will apply the patch and then create animated UIImage. The patch is because that `+[UIImage animatedImageWithImages:duration:]` just use the average of duration for each image. So it will not work if different frame has different duration. Therefore we repeat the specify frame for specify times to let it work.
For AppKit, NSImage does not support animates other than GIF. This will try to encode the frames to GIF format and then create an animated NSImage for rendering. Attention the animated image may loss some detail if the input frames contain full alpha channel because GIF only supports 1 bit alpha channel. (For 1 pixel, either transparent or not)
For AppKit before macOS 10.13, NSImage does not support animates other than GIF. This will try to encode the frames to GIF format and then create an animated NSImage for rendering. Attention the animated image may loss some detail if the input frames contain full alpha channel because GIF only supports 1 bit alpha channel. (For 1 pixel, either transparent or not)
For AppKit after macOS 10.14, use APNG format, which has a better render result.
Comment thread
Comment on lines +71 to +72

@coderabbitai coderabbitai Bot Feb 6, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Documentation gap for macOS 10.13.

The doc says "before macOS 10.13" uses GIF and "after macOS 10.14" uses APNG, but says nothing about macOS 10.13 itself. Since the @available check in the implementation uses macOS 10.14, macOS 10.13 falls into the GIF path. Consider rewording to "before macOS 10.14" for clarity and consistency with the implementation.

📝 Suggested doc fix
- For AppKit before macOS 10.13, NSImage does not support animates other than GIF. This will try to encode the frames to GIF format and then create an animated NSImage for rendering. Attention the animated image may loss some detail if the input frames contain full alpha channel because GIF only supports 1 bit alpha channel. (For 1 pixel, either transparent or not)
- For AppKit after macOS 10.14, use APNG format, which has a better render result.
+ For AppKit before macOS 10.14, NSImage does not support animates other than GIF. This will try to encode the frames to GIF format and then create an animated NSImage for rendering. Attention the animated image may loss some detail if the input frames contain full alpha channel because GIF only supports 1 bit alpha channel. (For 1 pixel, either transparent or not)
+ For AppKit on macOS 10.14+, use APNG format, which has a better render result.
🤖 Prompt for AI Agents
In `@SDWebImage/Core/SDImageCoderHelper.h` around lines 71 - 72, Update the doc
comment in SDImageCoderHelper.h to match the implementation's availability
check: change the "before macOS 10.13" phrasing to "before macOS 10.14" (or
rephrase to "macOS prior to 10.14") and indicate that APNG is used on macOS
10.14 and later so macOS 10.13 is clearly included in the GIF path; edit the
comment block around the NSImage/animate description to use "before macOS 10.14"
and "macOS 10.14 and later" to align with the `@available`(macOS 10.14, *) check
in the implementation.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@param frames The frames array. If no frames or frames is empty, return nil
@return A animated image for rendering on UIImageView(UIKit) or NSImageView(AppKit)
Expand Down
6 changes: 6 additions & 0 deletions 6 SDWebImage/Core/SDImageCoderHelper.m
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ + (UIImage *)animatedImageWithFrames:(NSArray<SDImageFrame *> *)frames {
#else

NSMutableData *imageData = [NSMutableData data];
SDImageFormat format;
if (@available(iOS 12.0, tvOS 12.0, macOS 10.14, watchOS 5.0, *)) {
format = SDImageFormatPNG;
} else {
format = SDImageFormatGIF;
}
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatGIF];
// Create an image destination. GIF does not support EXIF image orientation
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, imageUTType, frameCount, NULL);
Expand Down
6 changes: 5 additions & 1 deletion 6 Tests/Tests/SDImageCacheTests.m
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,11 @@ - (void)test42StoreCacheWithImageAndFormatWithoutImageData {
UIImage *diskImage = [SDImageCache.sharedImageCache imageFromDiskCacheForKey:kAnimatedImageKey5];
// Should save to GIF
expect(diskImage.sd_isAnimated).beTruthy();
expect(diskImage.sd_imageFormat).equal(SDImageFormatGIF);
if (@available(iOS 12.0, tvOS 12.0, macOS 10.14, watchOS 5.0, *)) {
expect(diskImage.sd_imageFormat).equal(SDImageFormatPNG);
} else {
expect(diskImage.sd_imageFormat).equal(SDImageFormatGIF);
}
[expectation5 fulfill];
}];

Expand Down
12 changes: 7 additions & 5 deletions 12 Tests/Tests/SDImageCoderTests.m
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -893,14 +893,16 @@ - (NSArray *)thumbnailImagesFromImageSource:(CGImageSourceRef)source API_AVAILAB
- (NSDictionary *)gainMapFromImageSource:(CGImageSourceRef)source {
if (@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *)) {
CFDictionaryRef ISOGainMap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeISOGainMap);
if (ISOGainMap) {
return (__bridge_transfer NSDictionary *)ISOGainMap;
}
CFDictionaryRef HDRGainMap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap);
NSDictionary *result = ISOGainMap ? (__bridge_transfer NSDictionary *)ISOGainMap : (__bridge_transfer NSDictionary *)HDRGainMap;
if (HDRGainMap) CFRelease(HDRGainMap);
if (ISOGainMap) CFRelease(ISOGainMap);
return result;
} else {
if (HDRGainMap) {
return (__bridge_transfer NSDictionary *)HDRGainMap;
}
return nil;
}
return nil;
}

#pragma mark - Utils
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.