SDAnimatedImageRep.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDAnimatedImageRep.h"
  9. #if SD_MAC
  10. #import "SDWebImageGIFCoder.h"
  11. @interface SDWebImageGIFCoder ()
  12. - (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source;
  13. @end
  14. @interface SDAnimatedImageRep ()
  15. @property (nonatomic, assign, readonly, nullable) CGImageSourceRef imageSource;
  16. @end
  17. @implementation SDAnimatedImageRep
  18. // `NSBitmapImageRep` will use `kCGImagePropertyGIFDelayTime` whenever you call `setProperty:withValue:` with `NSImageCurrentFrame` to change the current frame. We override it and use the actual `kCGImagePropertyGIFUnclampedDelayTime` if need.
  19. - (void)setProperty:(NSBitmapImageRepPropertyKey)property withValue:(id)value {
  20. [super setProperty:property withValue:value];
  21. if ([property isEqualToString:NSImageCurrentFrame]) {
  22. // Access the image source
  23. CGImageSourceRef imageSource = self.imageSource;
  24. if (!imageSource) {
  25. return;
  26. }
  27. // Check format type
  28. CFStringRef type = CGImageSourceGetType(imageSource);
  29. if (!type) {
  30. return;
  31. }
  32. NSUInteger index = [value unsignedIntegerValue];
  33. float frameDuration = 0;
  34. // Through we currently process GIF only, in the 5.x we support APNG so we keep the extensibility
  35. if (CFStringCompare(type, kUTTypeGIF, 0) == kCFCompareEqualTo) {
  36. frameDuration = [[SDWebImageGIFCoder sharedCoder] sd_frameDurationAtIndex:index source:imageSource];
  37. }
  38. if (!frameDuration) {
  39. return;
  40. }
  41. // Reset super frame duration with the actual frame duration
  42. [super setProperty:NSImageCurrentFrameDuration withValue:@(frameDuration)];
  43. }
  44. }
  45. - (CGImageSourceRef)imageSource {
  46. if (_tiffData) {
  47. return (__bridge CGImageSourceRef)(_tiffData);
  48. }
  49. return NULL;
  50. }
  51. @end
  52. #endif