-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
read and handle frame durations (delays) from GIF data
Also add comments to the header file, clean up the implementation generally, and add another test image with variable frame durations.
- Loading branch information
Rob Mayoff
committed
Feb 20, 2013
1 parent
94bf07c
commit 1b2cb8f
Showing
8 changed files
with
214 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,32 @@ | ||
#import <UIKit/UIKit.h> | ||
|
||
/** | ||
UIImage (animatedGIF) | ||
This category adds class methods to `UIImage` to create an animated `UIImage` from an animated GIF. | ||
*/ | ||
@interface UIImage (animatedGIF) | ||
|
||
+ (UIImage *)animatedImageWithAnimatedGIFData:(NSData *)data; | ||
+ (UIImage *)animatedImageWithAnimatedGIFURL:(NSURL *)url; | ||
/* | ||
UIImage *animation = [UIImage animatedImageWithAnimatedGIFData:theData]; | ||
I interpret `theData` as a GIF. I create an animated `UIImage` using the source images in the GIF. | ||
The GIF stores a separate duration for each frame, in units of centiseconds (hundredths of a second). However, a `UIImage` only has a single, total `duration` property, which is a floating-point number. | ||
To handle this mismatch, I add each source image (from the GIF) to `animation` a varying number of times to match the ratios between the frame durations in the GIF. | ||
For example, suppose the GIF contains three frames. Frame 0 has duration 3. Frame 1 has duration 9. Frame 2 has duration 15. I divide each duration by the greatest common denominator of all the durations, which is 3, and add each frame the resulting number of times. Thus `animation` will contain frame 0 3/3 = 1 time, then frame 1 9/3 = 3 times, then frame 2 15/3 = 5 times. I set `animation.duration` to (3+9+15)/100 = 0.27 seconds. | ||
*/ | ||
+ (UIImage *)animatedImageWithAnimatedGIFData:(NSData *)theData; | ||
|
||
/* | ||
UIImage *image = [UIImage animatedImageWithAnimatedGIFURL:theURL]; | ||
I interpret the contents of `theURL` as a GIF. I create an animated `UIImage` using the source images in the GIF. | ||
I operate exactly like `+[UIImage animatedImageWithAnimatedGIFData:]`, except that I read the data from `theURL`. If `theURL` is not a `file:` URL, you probably want to call me on a background thread or GCD queue to avoid blocking the main thread. | ||
*/ | ||
+ (UIImage *)animatedImageWithAnimatedGIFURL:(NSURL *)theURL; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.