ViewController.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // ViewController.m
  3. // TestGIF
  4. //
  5. // Created by huangyirong on 2020/6/2.
  6. // Copyright © 2020 Sogou. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. @import ImageIO;
  10. @interface ViewController ()
  11. @property (strong, nonatomic) UIImageView *imageView;
  12. @end
  13. @implementation ViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view.
  17. [self test1];
  18. }
  19. - (void)test1
  20. {
  21. _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
  22. [self.view addSubview:_imageView];
  23. NSString *path = [[NSBundle mainBundle] pathForResource:@"abc.gif" ofType:nil];
  24. NSData *data = [NSData dataWithContentsOfFile:path];
  25. // _imageView.image = [UIImage imageWithData:data];
  26. // return;
  27. CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
  28. NSInteger count = CGImageSourceGetCount(source);
  29. NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];
  30. for (int i = 0; i < count; i++) {
  31. CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, i, NULL);
  32. [images addObject:[UIImage imageWithCGImage:cgImage]];
  33. CGImageRelease(cgImage);
  34. }
  35. CFRelease(source);
  36. _imageView.animationImages = images;
  37. _imageView.animationDuration = count * 0.1;
  38. [_imageView startAnimating];
  39. }
  40. @end