SDAnimatedImagePlayer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <Foundation/Foundation.h>
  9. #import "SDWebImageCompat.h"
  10. #import "SDImageCoder.h"
  11. /// Animated image playback mode
  12. typedef NS_ENUM(NSUInteger, SDAnimatedImagePlaybackMode) {
  13. /**
  14. * From first to last frame and stop or next loop.
  15. */
  16. SDAnimatedImagePlaybackModeNormal = 0,
  17. /**
  18. * From last frame to first frame and stop or next loop.
  19. */
  20. SDAnimatedImagePlaybackModeReverse,
  21. /**
  22. * From first frame to last frame and reverse again, like reciprocating.
  23. */
  24. SDAnimatedImagePlaybackModeBounce,
  25. /**
  26. * From last frame to first frame and reverse again, like reversed reciprocating.
  27. */
  28. SDAnimatedImagePlaybackModeReversedBounce,
  29. };
  30. /// A player to control the playback of animated image, which can be used to drive Animated ImageView or any rendering usage, like CALayer/WatchKit/SwiftUI rendering.
  31. @interface SDAnimatedImagePlayer : NSObject
  32. /// Current playing frame image. This value is KVO Compliance.
  33. @property (nonatomic, readonly, nullable) UIImage *currentFrame;
  34. /// Current frame index, zero based. This value is KVO Compliance.
  35. @property (nonatomic, readonly) NSUInteger currentFrameIndex;
  36. /// Current loop count since its latest animating. This value is KVO Compliance.
  37. @property (nonatomic, readonly) NSUInteger currentLoopCount;
  38. /// Total frame count for animated image rendering. Defaults is animated image's frame count.
  39. /// @note For progressive animation, you can update this value when your provider receive more frames.
  40. @property (nonatomic, assign) NSUInteger totalFrameCount;
  41. /// Total loop count for animated image rendering. Default is animated image's loop count.
  42. @property (nonatomic, assign) NSUInteger totalLoopCount;
  43. /// The animation playback rate. Default is 1.0
  44. /// `1.0` means the normal speed.
  45. /// `0.0` means stopping the animation.
  46. /// `0.0-1.0` means the slow speed.
  47. /// `> 1.0` means the fast speed.
  48. /// `< 0.0` is not supported currently and stop animation. (may support reverse playback in the future)
  49. @property (nonatomic, assign) double playbackRate;
  50. /// Asynchronous setup animation playback mode. Default mode is SDAnimatedImagePlaybackModeNormal.
  51. @property (nonatomic, assign) SDAnimatedImagePlaybackMode playbackMode;
  52. /// Provide a max buffer size by bytes. This is used to adjust frame buffer count and can be useful when the decoding cost is expensive (such as Animated WebP software decoding). Default is 0.
  53. /// `0` means automatically adjust by calculating current memory usage.
  54. /// `1` means without any buffer cache, each of frames will be decoded and then be freed after rendering. (Lowest Memory and Highest CPU)
  55. /// `NSUIntegerMax` means cache all the buffer. (Lowest CPU and Highest Memory)
  56. @property (nonatomic, assign) NSUInteger maxBufferSize;
  57. /// You can specify a runloop mode to let it rendering.
  58. /// Default is NSRunLoopCommonModes on multi-core device, NSDefaultRunLoopMode on single-core device
  59. @property (nonatomic, copy, nonnull) NSRunLoopMode runLoopMode;
  60. /// Create a player with animated image provider. If the provider's `animatedImageFrameCount` is less than 1, returns nil.
  61. /// The provider can be any protocol implementation, like `SDAnimatedImage`, `SDImageGIFCoder`, etc.
  62. /// @note This provider can represent mutable content, like progressive animated loading. But you need to update the frame count by yourself
  63. /// @param provider The animated provider
  64. - (nullable instancetype)initWithProvider:(nonnull id<SDAnimatedImageProvider>)provider;
  65. /// Create a player with animated image provider. If the provider's `animatedImageFrameCount` is less than 1, returns nil.
  66. /// The provider can be any protocol implementation, like `SDAnimatedImage` or `SDImageGIFCoder`, etc.
  67. /// @note This provider can represent mutable content, like progressive animated loading. But you need to update the frame count by yourself
  68. /// @param provider The animated provider
  69. + (nullable instancetype)playerWithProvider:(nonnull id<SDAnimatedImageProvider>)provider;
  70. /// The handler block when current frame and index changed.
  71. @property (nonatomic, copy, nullable) void (^animationFrameHandler)(NSUInteger index, UIImage * _Nonnull frame);
  72. /// The handler block when one loop count finished.
  73. @property (nonatomic, copy, nullable) void (^animationLoopHandler)(NSUInteger loopCount);
  74. /// Return the status whether animation is playing.
  75. @property (nonatomic, readonly) BOOL isPlaying;
  76. /// Start the animation. Or resume the previously paused animation.
  77. - (void)startPlaying;
  78. /// Pause the animation. Keep the current frame index and loop count.
  79. - (void)pausePlaying;
  80. /// Stop the animation. Reset the current frame index and loop count.
  81. - (void)stopPlaying;
  82. /// Seek to the desired frame index and loop count.
  83. /// @note This can be used for advanced control like progressive loading, or skipping specify frames.
  84. /// @param index The frame index
  85. /// @param loopCount The loop count
  86. - (void)seekToFrameAtIndex:(NSUInteger)index loopCount:(NSUInteger)loopCount;
  87. /// Clear the frame cache buffer. The frame cache buffer size can be controlled by `maxBufferSize`.
  88. /// By default, when stop or pause the animation, the frame buffer is still kept to ready for the next restart
  89. - (void)clearFrameBuffer;
  90. @end