Skip to content

Commit

Permalink
swift api compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ibireme committed Feb 27, 2016
1 parent ba724ff commit d82174c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 101 deletions.
12 changes: 8 additions & 4 deletions Demo/YYTextDemo/YYImage/YYAnimatedImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

/**
An image view for displaying animated image.
Expand Down Expand Up @@ -38,7 +40,7 @@
The default value is `YES`.
*/
@property (nonatomic, assign) BOOL autoPlayAnimatedImage;
@property (nonatomic) BOOL autoPlayAnimatedImage;

/**
Index of the currently displayed frame (index from 0).
Expand All @@ -48,7 +50,7 @@
You can add an observer to this property to observe the playing status.
*/
@property (nonatomic, assign) NSUInteger currentAnimatedImageIndex;
@property (nonatomic) NSUInteger currentAnimatedImageIndex;

/**
Whether the image view is playing animation currently.
Expand Down Expand Up @@ -76,7 +78,7 @@
When receive memory warning or app enter background, the buffer will be released
immediately, and may grow back at the right time.
*/
@property (nonatomic, assign) NSUInteger maxBufferSize;
@property (nonatomic) NSUInteger maxBufferSize;

@end

Expand Down Expand Up @@ -107,7 +109,7 @@
/// Returns the frame image from a specified index.
/// This method may be called on background thread.
/// @param index Frame index (zero based).
- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index;
- (nullable UIImage *)animatedImageFrameAtIndex:(NSUInteger)index;

/// Returns the frames's duration from a specified index.
/// @param index Frame index (zero based).
Expand All @@ -119,3 +121,5 @@
/// It may used to display sprite animation with a single image (sprite sheet).
- (CGRect)animatedImageContentsRectAtIndex:(NSUInteger)index;
@end

NS_ASSUME_NONNULL_END
23 changes: 16 additions & 7 deletions Demo/YYTextDemo/YYImage/YYAnimatedImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ @interface YYAnimatedImageView() {
NSUInteger _curIndex; ///< current frame index (from 0)
NSUInteger _totalFrameCount; ///< total frame count

BOOL _loopEnd; ///< weather the loop is end.
BOOL _loopEnd; ///< whether the loop is end.
NSUInteger _curLoop; ///< current loop count (from 0)
NSUInteger _totalLoop; ///< total loop count, 0 means infinity

Expand Down Expand Up @@ -165,22 +165,28 @@ - (void)main {
if ([self isCancelled]) return;
view->_incrBufferCount++;
if (view->_incrBufferCount == 0) [view calcMaxBufferCount];
if ((int)view->_incrBufferCount > (int)view->_maxBufferCount) {
if (view->_incrBufferCount > (NSInteger)view->_maxBufferCount) {
view->_incrBufferCount = view->_maxBufferCount;
}
NSUInteger idx = _nextIndex;
NSUInteger max = view->_incrBufferCount < 1 ? 1 : view->_incrBufferCount;
NSUInteger total = view->_totalFrameCount;
view = nil;

for (int i = 0; i < max; i++, idx++) {
@autoreleasepool {
if (idx >= total) idx = 0;
if ([self isCancelled]) break;
__strong YYAnimatedImageView *view = _view;
if (!view) break;
LOCK_VIEW(BOOL miss = (view->_buffer[@(idx)] == nil));

if (miss) {
UIImage *img = [_curImage animatedImageFrameAtIndex:idx];
img = [img yy_imageByDecoded];
img = img.yy_imageByDecoded;
if ([self isCancelled]) break;
LOCK_VIEW(view->_buffer[@(idx)] = img ? img : [NSNull null]);
view = nil;
}
}
}
Expand Down Expand Up @@ -373,16 +379,18 @@ - (void)imageChanged {

// dynamically adjust buffer size for current memory.
- (void)calcMaxBufferCount {
NSUInteger bytes = _curAnimatedImage.animatedImageBytesPerFrame;
if (bytes == 0) bytes = 1;
int64_t bytes = (int64_t)_curAnimatedImage.animatedImageBytesPerFrame;
if (bytes == 0) bytes = 1024;

int64_t total = _YYDeviceMemoryTotal();
int64_t free = _YYDeviceMemoryFree();
int64_t max = MIN(total * 0.2, free * 0.6);
max = MAX(max, BUFFER_SIZE);
if (_maxBufferSize) max = max > _maxBufferSize ? _maxBufferSize : max;
_maxBufferCount = (float)max / (float)bytes;
if (_maxBufferCount == 0) _maxBufferCount = 1;
double maxBufferCount = (double)max / (double)bytes;
if (maxBufferCount < 1) maxBufferCount = 1;
else if (maxBufferCount > 512) maxBufferCount = 512;
_maxBufferCount = maxBufferCount;
}

- (void)dealloc {
Expand All @@ -398,6 +406,7 @@ - (BOOL)isAnimating {

- (void)stopAnimating {
[super stopAnimating];
[_requestQueue cancelAllOperations];
_link.paused = YES;
self.currentIsPlayingAnimation = NO;
}
Expand Down
20 changes: 16 additions & 4 deletions Demo/YYTextDemo/YYImage/YYFrameImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#import "YYAnimatedImageView.h"
#endif

NS_ASSUME_NONNULL_BEGIN

/**
An image to display frame-based animation.
Expand Down Expand Up @@ -49,7 +51,9 @@
@return An initialized YYFrameImage object, or nil when an error occurs.
*/
- (instancetype)initWithImagePaths:(NSArray *)paths oneFrameDuration:(NSTimeInterval)oneFrameDuration loopCount:(NSUInteger)loopCount;
- (nullable instancetype)initWithImagePaths:(NSArray<NSString *> *)paths
oneFrameDuration:(NSTimeInterval)oneFrameDuration
loopCount:(NSUInteger)loopCount;

/**
Create a frame animated image from files.
Expand All @@ -65,7 +69,9 @@
@return An initialized YYFrameImage object, or nil when an error occurs.
*/
- (instancetype)initWithImagePaths:(NSArray *)paths frameDurations:(NSArray *)frameDurations loopCount:(NSUInteger)loopCount;
- (nullable instancetype)initWithImagePaths:(NSArray<NSString *> *)paths
frameDurations:(NSArray<NSNumber *> *)frameDurations
loopCount:(NSUInteger)loopCount;

/**
Create a frame animated image from an array of data.
Expand All @@ -78,7 +84,9 @@
@return An initialized YYFrameImage object, or nil when an error occurs.
*/
- (instancetype)initWithImageDataArray:(NSArray *)dataArray oneFrameDuration:(NSTimeInterval)oneFrameDuration loopCount:(NSUInteger)loopCount;
- (nullable instancetype)initWithImageDataArray:(NSArray<NSData *> *)dataArray
oneFrameDuration:(NSTimeInterval)oneFrameDuration
loopCount:(NSUInteger)loopCount;

/**
Create a frame animated image from an array of data.
Expand All @@ -92,6 +100,10 @@
@return An initialized YYFrameImage object, or nil when an error occurs.
*/
- (instancetype)initWithImageDataArray:(NSArray *)dataArray frameDurations:(NSArray *)frameDurations loopCount:(NSUInteger)loopCount;
- (nullable instancetype)initWithImageDataArray:(NSArray<NSData *> *)dataArray
frameDurations:(NSArray *)frameDurations
loopCount:(NSUInteger)loopCount;

@end

NS_ASSUME_NONNULL_END
16 changes: 9 additions & 7 deletions Demo/YYTextDemo/YYImage/YYImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FOUNDATION_EXPORT const unsigned char YYImageVersionString[];
#import "YYAnimatedImageView.h"
#endif


NS_ASSUME_NONNULL_BEGIN


/**
Expand All @@ -53,10 +53,10 @@ FOUNDATION_EXPORT const unsigned char YYImageVersionString[];
*/
@interface YYImage : UIImage <YYAnimatedImage>

+ (YYImage *)imageNamed:(NSString *)name; // no cache!
+ (YYImage *)imageWithContentsOfFile:(NSString *)path;
+ (YYImage *)imageWithData:(NSData *)data;
+ (YYImage *)imageWithData:(NSData *)data scale:(CGFloat)scale;
+ (nullable YYImage *)imageNamed:(NSString *)name; // no cache!
+ (nullable YYImage *)imageWithContentsOfFile:(NSString *)path;
+ (nullable YYImage *)imageWithData:(NSData *)data;
+ (nullable YYImage *)imageWithData:(NSData *)data scale:(CGFloat)scale;

/**
If the image is created from data or file, then the value indicates the data type.
Expand All @@ -67,7 +67,7 @@ FOUNDATION_EXPORT const unsigned char YYImageVersionString[];
If the image is created from animated image data (multi-frame GIF/APNG/WebP),
this property stores the original image data.
*/
@property (nonatomic, readonly) NSData *animatedImageData;
@property (nullable, nonatomic, readonly) NSData *animatedImageData;

/**
The total memory usage (in bytes) if all frame images was loaded into memory.
Expand All @@ -85,6 +85,8 @@ FOUNDATION_EXPORT const unsigned char YYImageVersionString[];
See `animatedImageMemorySize` for memory cost.
*/
@property (nonatomic, assign) BOOL preloadAllAnimatedImageFrames;
@property (nonatomic) BOOL preloadAllAnimatedImageFrames;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit d82174c

Please sign in to comment.