YYImageDisplayExample.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // YYImageDisplayExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/8/9.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYImageDisplayExample.h"
  9. #import "YYImage.h"
  10. #import "UIView+YYAdd.h"
  11. #import "YYImageExampleHelper.h"
  12. #import <sys/sysctl.h>
  13. @interface YYImageDisplayExample()<UIGestureRecognizerDelegate>
  14. @end
  15. @implementation YYImageDisplayExample {
  16. UIScrollView *_scrollView;
  17. }
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. self.view.backgroundColor = [UIColor colorWithWhite:0.863 alpha:1.000];
  21. _scrollView = [UIScrollView new];
  22. _scrollView.frame = self.view.bounds;
  23. [self.view addSubview:_scrollView];
  24. UILabel *label = [UILabel new];
  25. label.backgroundColor = [UIColor clearColor];
  26. label.size = CGSizeMake(self.view.width, 60);
  27. label.top = 20;
  28. label.textAlignment = NSTextAlignmentCenter;
  29. label.numberOfLines = 0;
  30. label.text = @"Tap the image to pause/play\n Slide on the image to forward/rewind";
  31. if ([self isSimulator]) {
  32. label.text = [@"Please run this app in device\nto get better performance.\n\n" stringByAppendingString:label.text];
  33. label.height = 120;
  34. }
  35. [_scrollView addSubview:label];
  36. [self addImageWithName:@"niconiconi" text:@"Animated GIF"];
  37. [self addImageWithName:@"wall-e" text:@"Animated WebP"];
  38. [self addImageWithName:@"pia" text:@"Animated PNG (APNG)"];
  39. [self addFrameImageWithText:@"Frame Animation"];
  40. [self addSpriteSheetImageWithText:@"Sprite Sheet Animation"];
  41. _scrollView.panGestureRecognizer.cancelsTouchesInView = YES;
  42. }
  43. - (void)addImageWithName:(NSString *)name text:(NSString *)text {
  44. YYImage *image = [YYImage imageNamed:name];
  45. [self addImage:image size:CGSizeZero text:text];
  46. }
  47. - (void)addFrameImageWithText:(NSString *)text {
  48. NSString *basePath = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:@"EmoticonWeibo.bundle/com.sina.default"];
  49. NSMutableArray *paths = [NSMutableArray new];
  50. [paths addObject:[basePath stringByAppendingPathComponent:@"d_aini@3x.png"]];
  51. [paths addObject:[basePath stringByAppendingPathComponent:@"d_baibai@3x.png"]];
  52. [paths addObject:[basePath stringByAppendingPathComponent:@"d_chanzui@3x.png"]];
  53. [paths addObject:[basePath stringByAppendingPathComponent:@"d_chijing@3x.png"]];
  54. [paths addObject:[basePath stringByAppendingPathComponent:@"d_dahaqi@3x.png"]];
  55. [paths addObject:[basePath stringByAppendingPathComponent:@"d_guzhang@3x.png"]];
  56. [paths addObject:[basePath stringByAppendingPathComponent:@"d_haha@2x.png"]];
  57. [paths addObject:[basePath stringByAppendingPathComponent:@"d_haixiu@3x.png"]];
  58. UIImage *image = [[YYFrameImage alloc] initWithImagePaths:paths oneFrameDuration:0.1 loopCount:0];
  59. [self addImage:image size:CGSizeZero text:text];
  60. }
  61. - (void)addSpriteSheetImageWithText:(NSString *)text {
  62. NSString *path = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:@"ResourceTwitter.bundle/fav02l-sheet@2x.png"];
  63. UIImage *sheet = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:path] scale:2];
  64. NSMutableArray *contentRects = [NSMutableArray new];
  65. NSMutableArray *durations = [NSMutableArray new];
  66. // 8 * 12 sprites in a single sheet image
  67. CGSize size = CGSizeMake(sheet.size.width / 8, sheet.size.height / 12);
  68. for (int j = 0; j < 12; j++) {
  69. for (int i = 0; i < 8; i++) {
  70. CGRect rect;
  71. rect.size = size;
  72. rect.origin.x = sheet.size.width / 8 * i;
  73. rect.origin.y = sheet.size.height / 12 * j;
  74. [contentRects addObject:[NSValue valueWithCGRect:rect]];
  75. [durations addObject:@(1 / 60.0)];
  76. }
  77. }
  78. YYSpriteSheetImage *sprite;
  79. sprite = [[YYSpriteSheetImage alloc] initWithSpriteSheetImage:sheet
  80. contentRects:contentRects
  81. frameDurations:durations
  82. loopCount:0];
  83. [self addImage:sprite size:size text:text];
  84. }
  85. - (void)addImage:(UIImage *)image size:(CGSize)size text:(NSString *)text {
  86. YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
  87. if (size.width > 0 && size.height > 0) imageView.size = size;
  88. imageView.centerX = self.view.width / 2;
  89. imageView.top = [(UIView *)[_scrollView.subviews lastObject] bottom] + 30;
  90. [_scrollView addSubview:imageView];
  91. [YYImageExampleHelper addTapControlToAnimatedImageView:imageView];
  92. [YYImageExampleHelper addPanControlToAnimatedImageView:imageView];
  93. for (UIGestureRecognizer *g in imageView.gestureRecognizers) {
  94. g.delegate = self;
  95. }
  96. UILabel *imageLabel = [UILabel new];
  97. imageLabel.backgroundColor = [UIColor clearColor];
  98. imageLabel.frame = CGRectMake(0, 0, self.view.width, 20);
  99. imageLabel.top = imageView.bottom + 10;
  100. imageLabel.textAlignment = NSTextAlignmentCenter;
  101. imageLabel.text = text;
  102. [_scrollView addSubview:imageLabel];
  103. _scrollView.contentSize = CGSizeMake(self.view.width, imageLabel.bottom + 20);
  104. }
  105. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  106. return YES;
  107. }
  108. - (BOOL)isSimulator {
  109. size_t size;
  110. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  111. char *machine = malloc(size);
  112. sysctlbyname("hw.machine", machine, &size, NULL, 0);
  113. NSString *model = [NSString stringWithUTF8String:machine];
  114. free(machine);
  115. return [model isEqualToString:@"x86_64"] || [model isEqualToString:@"i386"];
  116. }
  117. @end