Merge pull request #2847 from dreampiggy/bugfix_mac_gif_loop_count

Fix the macOS SDAnimatedImageRep to match Netscape standard of GIF loop count, which should use 1 when there are no loop count information
This commit is contained in:
DreamPiggy 2019-09-21 14:15:39 +08:00 committed by GitHub
commit 20c8adc22d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 9 deletions

View File

@ -11,7 +11,8 @@
#if SD_MAC
/**
A subclass of `NSBitmapImageRep` to fix that GIF loop count issue because `NSBitmapImageRep` will reset `NSImageCurrentFrameDuration` by using `kCGImagePropertyGIFDelayTime` but not `kCGImagePropertyGIFUnclampedDelayTime`.
A subclass of `NSBitmapImageRep` to fix that GIF duration issue because `NSBitmapImageRep` will reset `NSImageCurrentFrameDuration` by using `kCGImagePropertyGIFDelayTime` but not `kCGImagePropertyGIFUnclampedDelayTime`.
This also fix the GIF loop count issue, which will use the Netscape standard (See http://www6.uniovi.es/gifanim/gifabout.htm) to only place once when the `kCGImagePropertyGIFLoopCount` is nil. This is what modern browser's behavior.
Built in GIF coder use this instead of `NSBitmapImageRep` for better GIF rendering. If you do not want this, only enable `SDImageIOCoder`, which just call `NSImage` API and actually use `NSBitmapImageRep` for GIF image.
This also support APNG format using `SDImageAPNGCoder`. Which provide full alpha-channel support and the correct duration match the `kCGImagePropertyAPNGUnclampedDelayTime`.
*/

View File

@ -50,7 +50,10 @@
}
if (CFStringCompare(type, kUTTypeGIF, 0) == kCFCompareEqualTo) {
// GIF
// Do nothing because NSBitmapImageRep support it
// Fix the `NSBitmapImageRep` GIF loop count calculation issue
// Which will use 0 when there are no loop count information metadata in GIF data
NSUInteger loopCount = [SDImageGIFCoder imageLoopCountWithSource:imageSource];
[self setProperty:NSImageLoopCount withValue:@(loopCount)];
} else if (CFStringCompare(type, kUTTypePNG, 0) == kCFCompareEqualTo) {
// APNG
// Do initilize about frame count, current frame/duration and loop count

View File

@ -103,12 +103,7 @@
NSString * testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestLoopCount" ofType:@"gif"];
NSData *testImageData = [NSData dataWithContentsOfFile:testImagePath];
UIImage *image = [SDImageGIFCoder.sharedCoder decodedImageWithData:testImageData options:nil];
#if SD_MAC
// TODO, macOS's `NSBitmapImageRep` treate this loop count as 0, this need to be fixed in next PR.
expect(image.sd_imageLoopCount).equal(0);
#else
expect(image.sd_imageLoopCount).equal(1);
#endif
}
- (void)test13ThatHEICWorks {
@ -194,8 +189,6 @@
- (void)test16ThatImageIOAnimatedCoderAbstractClass {
SDImageIOAnimatedCoder *coder = [[SDImageIOAnimatedCoder alloc] init];
NSString * testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImage" ofType:@"png"];
NSData *testImageData = [NSData dataWithContentsOfFile:testImagePath];
@try {
[coder canEncodeToFormat:SDImageFormatPNG];
XCTFail("Should throw exception");