YYWebImageExample.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // YYWebImageExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/7/19.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYWebImageExample.h"
  9. #import <YYKit/YYKit.h>
  10. #define kCellHeight ceil((kScreenWidth) * 3.0 / 4.0)
  11. @interface YYWebImageExampleCell : UITableViewCell
  12. @property (nonatomic, strong) YYAnimatedImageView *webImageView;
  13. @property (nonatomic, strong) UIActivityIndicatorView *indicator;
  14. @property (nonatomic, strong) CAShapeLayer *progressLayer;
  15. @property (nonatomic, strong) UILabel *label;
  16. @end
  17. @implementation YYWebImageExampleCell
  18. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  19. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  20. self.backgroundColor = [UIColor clearColor];
  21. self.contentView.backgroundColor = [UIColor clearColor];
  22. self.size = CGSizeMake(kScreenWidth, kCellHeight);
  23. self.contentView.size = self.size;
  24. _webImageView = [YYAnimatedImageView new];
  25. _webImageView.size = self.size;
  26. _webImageView.clipsToBounds = YES;
  27. _webImageView.contentMode = UIViewContentModeScaleAspectFill;
  28. _webImageView.backgroundColor = [UIColor whiteColor];
  29. [self.contentView addSubview:_webImageView];
  30. _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  31. _indicator.center = CGPointMake(self.width / 2, self.height / 2);
  32. _indicator.hidden = YES;
  33. //[self.contentView addSubview:_indicator]; //use progress bar instead..
  34. _label = [UILabel new];
  35. _label.size = self.size;
  36. _label.textAlignment = NSTextAlignmentCenter;
  37. _label.text = @"Load fail, tap to reload.";
  38. _label.textColor = [UIColor colorWithWhite:0.7 alpha:1.0];
  39. _label.hidden = YES;
  40. _label.userInteractionEnabled = YES;
  41. [self.contentView addSubview:_label];
  42. CGFloat lineHeight = 4;
  43. _progressLayer = [CAShapeLayer layer];
  44. _progressLayer.size = CGSizeMake(_webImageView.width, lineHeight);
  45. UIBezierPath *path = [UIBezierPath bezierPath];
  46. [path moveToPoint:CGPointMake(0, _progressLayer.height / 2)];
  47. [path addLineToPoint:CGPointMake(_webImageView.width, _progressLayer.height / 2)];
  48. _progressLayer.lineWidth = lineHeight;
  49. _progressLayer.path = path.CGPath;
  50. _progressLayer.strokeColor = [UIColor colorWithRed:0.000 green:0.640 blue:1.000 alpha:0.720].CGColor;
  51. _progressLayer.lineCap = kCALineCapButt;
  52. _progressLayer.strokeStart = 0;
  53. _progressLayer.strokeEnd = 0;
  54. [_webImageView.layer addSublayer:_progressLayer];
  55. __weak typeof(self) _self = self;
  56. UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
  57. [_self setImageURL:_self.webImageView.imageURL];
  58. }];
  59. [_label addGestureRecognizer:g];
  60. return self;
  61. }
  62. - (void)setImageURL:(NSURL *)url {
  63. _label.hidden = YES;
  64. _indicator.hidden = NO;
  65. [_indicator startAnimating];
  66. __weak typeof(self) _self = self;
  67. [CATransaction begin];
  68. [CATransaction setDisableActions: YES];
  69. self.progressLayer.hidden = YES;
  70. self.progressLayer.strokeEnd = 0;
  71. [CATransaction commit];
  72. [_webImageView setImageWithURL:url
  73. placeholder:nil
  74. options:YYWebImageOptionProgressiveBlur | YYWebImageOptionShowNetworkActivity | YYWebImageOptionSetImageWithFadeAnimation
  75. progress:^(NSInteger receivedSize, NSInteger expectedSize) {
  76. if (expectedSize > 0 && receivedSize > 0) {
  77. CGFloat progress = (CGFloat)receivedSize / expectedSize;
  78. progress = progress < 0 ? 0 : progress > 1 ? 1 : progress;
  79. if (_self.progressLayer.hidden) _self.progressLayer.hidden = NO;
  80. _self.progressLayer.strokeEnd = progress;
  81. }
  82. } transform:nil
  83. completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
  84. if (stage == YYWebImageStageFinished) {
  85. _self.progressLayer.hidden = YES;
  86. [_self.indicator stopAnimating];
  87. _self.indicator.hidden = YES;
  88. if (!image) _self.label.hidden = NO;
  89. }
  90. }];
  91. }
  92. - (void)prepareForReuse {
  93. //nothing
  94. }
  95. @end
  96. @implementation YYWebImageExample {
  97. NSArray *_imageLinks;
  98. }
  99. - (void)viewDidLoad {
  100. [super viewDidLoad];
  101. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  102. self.view.backgroundColor = [UIColor whiteColor];
  103. UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Reload" style:UIBarButtonItemStylePlain target:self action:@selector(reload)];
  104. self.navigationItem.rightBarButtonItem = button;
  105. self.view.backgroundColor = [UIColor colorWithWhite:0.217 alpha:1.000];
  106. NSArray *links = @[
  107. /*
  108. You can add your image url here.
  109. */
  110. // progressive jpeg
  111. @"https://s-media-cache-ak0.pinimg.com/1200x/2e/0c/c5/2e0cc5d86e7b7cd42af225c29f21c37f.jpg",
  112. // animated gif: http://cinemagraphs.com/
  113. @"http://i.imgur.com/uoBwCLj.gif",
  114. @"http://i.imgur.com/8KHKhxI.gif",
  115. @"http://i.imgur.com/WXJaqof.gif",
  116. // animated gif: https://dribbble.com/markpear
  117. @"https://d13yacurqjgara.cloudfront.net/users/345826/screenshots/1780193/dots18.gif",
  118. @"https://d13yacurqjgara.cloudfront.net/users/345826/screenshots/1809343/dots17.1.gif",
  119. @"https://d13yacurqjgara.cloudfront.net/users/345826/screenshots/1845612/dots22.gif",
  120. @"https://d13yacurqjgara.cloudfront.net/users/345826/screenshots/1820014/big-hero-6.gif",
  121. @"https://d13yacurqjgara.cloudfront.net/users/345826/screenshots/1819006/dots11.0.gif",
  122. @"https://d13yacurqjgara.cloudfront.net/users/345826/screenshots/1799885/dots21.gif",
  123. // animaged gif: https://dribbble.com/jonadinges
  124. @"https://d13yacurqjgara.cloudfront.net/users/288987/screenshots/2025999/batman-beyond-the-rain.gif",
  125. @"https://d13yacurqjgara.cloudfront.net/users/288987/screenshots/1855350/r_nin.gif",
  126. @"https://d13yacurqjgara.cloudfront.net/users/288987/screenshots/1963497/way-back-home.gif",
  127. @"https://d13yacurqjgara.cloudfront.net/users/288987/screenshots/1913272/depressed-slurp-cycle.gif",
  128. // jpg: https://dribbble.com/snootyfox
  129. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/2047158/beerhenge.jpg",
  130. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/2016158/avalanche.jpg",
  131. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1839353/pilsner.jpg",
  132. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1833469/porter.jpg",
  133. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1521183/farmers.jpg",
  134. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1391053/tents.jpg",
  135. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1399501/imperial_beer.jpg",
  136. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1488711/fishin.jpg",
  137. @"https://d13yacurqjgara.cloudfront.net/users/26059/screenshots/1466318/getaway.jpg",
  138. // animated webp and apng: http://littlesvr.ca/apng/gif_apng_webp.html
  139. @"http://littlesvr.ca/apng/images/BladeRunner.png",
  140. @"http://littlesvr.ca/apng/images/Contact.webp",
  141. ];
  142. _imageLinks = links;
  143. [self.tableView reloadData];
  144. [self scrollViewDidScroll:self.tableView];
  145. }
  146. - (void)viewDidAppear:(BOOL)animated {
  147. [super viewDidAppear:animated];
  148. if (kiOS7Later) {
  149. self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  150. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  151. }
  152. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  153. }
  154. - (void)viewWillDisappear:(BOOL)animated {
  155. [super viewWillDisappear:animated];
  156. if (kiOS7Later) {
  157. self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  158. self.navigationController.navigationBar.tintColor = nil;
  159. }
  160. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  161. }
  162. - (void)reload {
  163. [[YYImageCache sharedCache].memoryCache removeAllObjects];
  164. [[YYImageCache sharedCache].diskCache removeAllObjectsWithBlock:nil];
  165. [self.tableView performSelector:@selector(reloadData) afterDelay:0.1];
  166. }
  167. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  168. return NO;
  169. }
  170. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  171. return _imageLinks.count * 4;
  172. }
  173. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  174. return kCellHeight;
  175. }
  176. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  177. YYWebImageExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  178. if (!cell) cell = [[YYWebImageExampleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  179. [cell setImageURL:[NSURL URLWithString:_imageLinks[indexPath.row % _imageLinks.count]]];
  180. return cell;
  181. }
  182. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  183. CGFloat viewHeight = scrollView.height + scrollView.contentInset.top;
  184. for (YYWebImageExampleCell *cell in [self.tableView visibleCells]) {
  185. CGFloat y = cell.centerY - scrollView.contentOffset.y;
  186. CGFloat p = y - viewHeight / 2;
  187. CGFloat scale = cos(p / viewHeight * 0.8) * 0.95;
  188. if (kiOS8Later) {
  189. [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState animations:^{
  190. cell.webImageView.transform = CGAffineTransformMakeScale(scale, scale);
  191. } completion:NULL];
  192. } else {
  193. cell.webImageView.transform = CGAffineTransformMakeScale(scale, scale);
  194. }
  195. }
  196. }
  197. @end