YYWebImageExample.m 10 KB

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