Skip to content

Commit cba8ec9

Browse files
authored
Merge pull request SDWebImage#3727 from dreampiggy/bugfix/heics_animated_encoding
Fix our HEIC coder to encode `timed image sequences` instead of `non-timed image gallery` for HEIC encoding
2 parents b26c962 + 11dc8e8 commit cba8ec9

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

SDWebImage/Core/SDImageHEICCoder.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ + (SDImageFormat)imageFormat {
7575
}
7676

7777
+ (NSString *)imageUTType {
78-
return (__bridge NSString *)kSDUTTypeHEIC;
78+
// See: https://nokiatech.github.io/heif/technical.html
79+
// Actually HEIC has another concept called `non-timed Image Sequence`, which can be encoded using `public.heic`
80+
// But current SDWebImage does not has this design, I don't know whether there are use case for this
81+
// So we just replace and always use `timed Image Sequence`, means, animated image for encoding
82+
return (__bridge NSString *)kSDUTTypeHEICS;
7983
}
8084

8185
+ (NSString *)dictionaryProperty {

SDWebImage/Core/SDImageIOAnimatedCoder.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,11 +832,11 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
832832
}
833833

834834
NSMutableData *imageData = [NSMutableData data];
835-
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:format];
835+
NSString *imageUTType = self.class.imageUTType;
836836

837837
// Create an image destination. Animated Image does not support EXIF image orientation TODO
838838
// The `CGImageDestinationCreateWithData` will log a warning when count is 0, use 1 instead.
839-
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, imageUTType, frames.count ?: 1, NULL);
839+
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, (__bridge CFStringRef)imageUTType, frames.count ?: 1, NULL);
840840
if (!imageDestination) {
841841
// Handle failure.
842842
return nil;
@@ -922,6 +922,11 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
922922

923923
CFRelease(imageDestination);
924924

925+
// In some beta version, ImageIO `CGImageDestinationFinalize` returns success, but the data buffer is 0 bytes length.
926+
if (imageData.length == 0) {
927+
return nil;
928+
}
929+
925930
return [imageData copy];
926931
}
927932

0 commit comments

Comments
 (0)