PINAnimatedImage.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // PINAnimatedImage.h
  3. // Pods
  4. //
  5. // Created by Garrett Moon on 3/18/16.
  6. //
  7. //
  8. #import <Foundation/Foundation.h>
  9. #if PIN_TARGET_IOS
  10. #import <UIKit/UIKit.h>
  11. #elif PIN_TARGET_MAC
  12. #import <Cocoa/Cocoa.h>
  13. #endif
  14. #import "PINRemoteImageMacros.h"
  15. #define PINAnimatedImageDebug 0
  16. extern NSString *kPINAnimatedImageErrorDomain;
  17. /**
  18. PINAnimatedImage decoding and processing errors.
  19. */
  20. typedef NS_ENUM(NSUInteger, PINAnimatedImageError) {
  21. /** No error, yay! */
  22. PINAnimatedImageErrorNoError = 0,
  23. /** Could not create a necessary file. */
  24. PINAnimatedImageErrorFileCreationError,
  25. /** Could not get a file handle to the necessary file. */
  26. PINAnimatedImageErrorFileHandleError,
  27. /** Could not decode the image. */
  28. PINAnimatedImageErrorImageFrameError,
  29. /** Could not memory map the file. */
  30. PINAnimatedImageErrorMappingError,
  31. /** File write error */
  32. PINAnimatedImageErrorFileWrite,
  33. };
  34. /**
  35. The processing status of the animated image.
  36. */
  37. typedef NS_ENUM(NSUInteger, PINAnimatedImageStatus) {
  38. /** No work has been done. */
  39. PINAnimatedImageStatusUnprocessed = 0,
  40. /** Info about the animated image and the cover image are available. */
  41. PINAnimatedImageStatusInfoProcessed,
  42. /** At least one set of frames has been decoded to a file. It's safe to start playback. */
  43. PINAnimatedImageStatusFirstFileProcessed,
  44. /** The entire animated image has been processed. */
  45. PINAnimatedImageStatusProcessed,
  46. /** Processing was canceled. */
  47. PINAnimatedImageStatusCanceled,
  48. /** There was an error in processing. */
  49. PINAnimatedImageStatusError,
  50. };
  51. extern const Float32 kPINAnimatedImageDefaultDuration;
  52. extern const Float32 kPINAnimatedImageMinimumDuration;
  53. extern const NSTimeInterval kPINAnimatedImageDisplayRefreshRate;
  54. /**
  55. Called when the cover image of an animatedImage is ready.
  56. */
  57. typedef void(^PINAnimatedImageInfoReady)(PINImage *coverImage);
  58. /**
  59. PINAnimatedImage is a class which decodes GIFs to memory mapped files on disk. Like PINRemoteImageManager,
  60. it will only decode a GIF one time, regardless of the number of the number of PINAnimatedImages created with
  61. the same NSData.
  62. PINAnimatedImage's are also decoded chunks at a time, writing each chunk to a separate file. This allows callback
  63. and playback to start before the GIF is completely decoded. If a frame is requested beyond what has been processed,
  64. nil will be returned. Because a fileReady is called on each chunk completion, you can pause playback if you hit a nil
  65. frame until you receive another fileReady call.
  66. Internally, PINAnimatedImage attempts to keep only the files it needs open – the last file associated with the requested
  67. frame and the one after (to prime).
  68. It's important to note that until infoCompletion is called, it is unsafe to access many of the methods on PINAnimatedImage.
  69. */
  70. @interface PINAnimatedImage : NSObject
  71. - (instancetype)initWithAnimatedImageData:(NSData *)animatedImageData NS_DESIGNATED_INITIALIZER;
  72. /**
  73. A block to be called on when GIF info has been processed. Status will == PINAnimatedImageStatusInfoProcessed
  74. */
  75. @property (nonatomic, strong, readwrite) PINAnimatedImageInfoReady infoCompletion;
  76. /**
  77. A block to be called whenever a new file is done being processed. You can start (or resume) playback when you
  78. get this callback, though it's possible for playback to catch up to the decoding and you'll need to pause.
  79. */
  80. @property (nonatomic, strong, readwrite) dispatch_block_t fileReady;
  81. /**
  82. A block to be called when the animated image is fully decoded and written to disk.
  83. */
  84. @property (nonatomic, strong, readwrite) dispatch_block_t animatedImageReady;
  85. /**
  86. The current status of the animated image.
  87. */
  88. @property (nonatomic, assign, readwrite) PINAnimatedImageStatus status;
  89. /**
  90. A helper function which references status to check if the coverImage is ready.
  91. */
  92. @property (nonatomic, readonly) BOOL coverImageReady;
  93. /**
  94. A helper function which references status to check if playback is ready.
  95. */
  96. @property (nonatomic, readonly) BOOL playbackReady;
  97. /**
  98. The first frame / cover image of the animated image.
  99. @warning Access to this property before status == PINAnimatedImageStatusInfoProcessed is undefined. You can check coverImageReady too.
  100. */
  101. @property (nonatomic, readonly) PINImage *coverImage;
  102. /**
  103. The total duration of one loop of playback.
  104. @warning Access to this property before status == PINAnimatedImageStatusInfoProcessed is undefined.
  105. */
  106. @property (nonatomic, readonly) CFTimeInterval totalDuration;
  107. /**
  108. The number of frames to play per second * display refresh rate (defined as 60 which appears to be true on iOS). You probably want to
  109. set this value on a displayLink.
  110. @warning Access to this property before status == PINAnimatedImageStatusInfoProcessed is undefined.
  111. */
  112. @property (nonatomic, readonly) NSUInteger frameInterval;
  113. /**
  114. The number of times to loop the animated image. Returns 0 if looping should occur infinitely.
  115. @warning Access to this property before status == PINAnimatedImageStatusInfoProcessed is undefined.
  116. */
  117. @property (nonatomic, readonly) size_t loopCount;
  118. /**
  119. The total number of frames in the animated image.
  120. @warning Access to this property before status == PINAnimatedImageStatusInfoProcessed is undefined.
  121. */
  122. @property (nonatomic, readonly) size_t frameCount;
  123. /**
  124. Any processing error that may have occured.
  125. */
  126. @property (nonatomic, readonly) NSError *error;
  127. /**
  128. The image at the frame index passed in.
  129. @param index The index of the frame to retrieve.
  130. @warning Access to this property before status == PINAnimatedImageStatusInfoProcessed is undefined.
  131. */
  132. - (CGImageRef)imageAtIndex:(NSUInteger)index;
  133. /**
  134. The duration of the frame of the passed in index.
  135. @param index The index of the frame to retrieve the duration it should be shown for.
  136. @warning Access to this property before status == PINAnimatedImageStatusInfoProcessed is undefined.
  137. */
  138. - (CFTimeInterval)durationAtIndex:(NSUInteger)index;
  139. /**
  140. Clears out the strong references to any memory maps that are being held.
  141. */
  142. - (void)clearAnimatedImageCache;
  143. @end