UIImage+animatedGIF.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // UIImage+animatedGIF
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/05/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. // Found in Internet
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. #import <Foundation/Foundation.h>
  25. #import <UIKit/UIKit.h>
  26. #import <ImageIO/ImageIO.h>
  27. @interface UIImage (animatedGIF)
  28. /*
  29. UIImage *animation = [UIImage animatedImageWithAnimatedGIFData:theData];
  30. I interpret `theData` as a GIF. I create an animated `UIImage` using the source images in the GIF.
  31. 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.
  32. 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.
  33. 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.
  34. */
  35. + (UIImage *)animatedImageWithAnimatedGIFData:(NSData *)theData;
  36. /*
  37. UIImage *image = [UIImage animatedImageWithAnimatedGIFURL:theURL];
  38. I interpret the contents of `theURL` as a GIF. I create an animated `UIImage` using the source images in the GIF.
  39. 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.
  40. */
  41. + (UIImage *)animatedImageWithAnimatedGIFURL:(NSURL *)theURL;
  42. @end