FLAnimatedImage.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // FLAnimatedImage.h
  3. // Flipboard
  4. //
  5. // Created by Raphael Schaad on 7/8/13.
  6. // Copyright (c) 2013-2015 Flipboard. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. // Allow user classes conveniently just importing one header.
  10. #import "FLAnimatedImageView.h"
  11. #ifndef NS_DESIGNATED_INITIALIZER
  12. #if __has_attribute(objc_designated_initializer)
  13. #define NS_DESIGNATED_INITIALIZER __attribute((objc_designated_initializer))
  14. #else
  15. #define NS_DESIGNATED_INITIALIZER
  16. #endif
  17. #endif
  18. extern const NSTimeInterval kFLAnimatedImageDelayTimeIntervalMinimum;
  19. //
  20. // An `FLAnimatedImage`'s job is to deliver frames in a highly performant way and works in conjunction with `FLAnimatedImageView`.
  21. // It subclasses `NSObject` and not `UIImage` because it's only an "image" in the sense that a sea lion is a lion.
  22. // It tries to intelligently choose the frame cache size depending on the image and memory situation with the goal to lower CPU usage for smaller ones, lower memory usage for larger ones and always deliver frames for high performant play-back.
  23. // Note: `posterImage`, `size`, `loopCount`, `delayTimes` and `frameCount` don't change after successful initialization.
  24. //
  25. @interface FLAnimatedImage : NSObject
  26. @property (nonatomic, strong, readonly) UIImage *posterImage; // Guaranteed to be loaded; usually equivalent to `-imageLazilyCachedAtIndex:0`
  27. @property (nonatomic, assign, readonly) CGSize size; // The `.posterImage`'s `.size`
  28. @property (nonatomic, assign, readonly) NSUInteger loopCount; // 0 means repeating the animation indefinitely
  29. @property (nonatomic, strong, readonly) NSDictionary *delayTimesForIndexes; // Of type `NSTimeInterval` boxed in `NSNumber`s
  30. @property (nonatomic, assign, readonly) NSUInteger frameCount; // Number of valid frames; equal to `[.delayTimes count]`
  31. @property (nonatomic, assign, readonly) NSUInteger frameCacheSizeCurrent; // Current size of intelligently chosen buffer window; can range in the interval [1..frameCount]
  32. @property (nonatomic, assign) NSUInteger frameCacheSizeMax; // Allow to cap the cache size; 0 means no specific limit (default)
  33. // Intended to be called from main thread synchronously; will return immediately.
  34. // If the result isn't cached, will return `nil`; the caller should then pause playback, not increment frame counter and keep polling.
  35. // After an initial loading time, depending on `frameCacheSize`, frames should be available immediately from the cache.
  36. - (UIImage *)imageLazilyCachedAtIndex:(NSUInteger)index;
  37. // Pass either a `UIImage` or an `FLAnimatedImage` and get back its size
  38. + (CGSize)sizeForImage:(id)image;
  39. // On success, the initializers return an `FLAnimatedImage` with all fields initialized, on failure they return `nil` and an error will be logged.
  40. - (instancetype)initWithAnimatedGIFData:(NSData *)data;
  41. // Pass 0 for optimalFrameCacheSize to get the default, predrawing is enabled by default.
  42. - (instancetype)initWithAnimatedGIFData:(NSData *)data optimalFrameCacheSize:(NSUInteger)optimalFrameCacheSize predrawingEnabled:(BOOL)isPredrawingEnabled NS_DESIGNATED_INITIALIZER;
  43. + (instancetype)animatedImageWithGIFData:(NSData *)data;
  44. @property (nonatomic, strong, readonly) NSData *data; // The data the receiver was initialized with; read-only
  45. @end
  46. typedef NS_ENUM(NSUInteger, FLLogLevel) {
  47. FLLogLevelNone = 0,
  48. FLLogLevelError,
  49. FLLogLevelWarn,
  50. FLLogLevelInfo,
  51. FLLogLevelDebug,
  52. FLLogLevelVerbose
  53. };
  54. @interface FLAnimatedImage (Logging)
  55. + (void)setLogBlock:(void (^)(NSString *logString, FLLogLevel logLevel))logBlock logLevel:(FLLogLevel)logLevel;
  56. + (void)logStringFromBlock:(NSString *(^)(void))stringBlock withLevel:(FLLogLevel)level;
  57. @end
  58. #define FLLog(logLevel, format, ...) [FLAnimatedImage logStringFromBlock:^NSString *{ return [NSString stringWithFormat:(format), ## __VA_ARGS__]; } withLevel:(logLevel)]
  59. @interface FLWeakProxy : NSProxy
  60. + (instancetype)weakProxyForObject:(id)targetObject;
  61. @end