DetailViewController.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "DetailViewController.h"
  9. #import <SDWebImage/FLAnimatedImageView.h>
  10. #import <SDWebImage/FLAnimatedImageView+WebCache.h>
  11. @interface DetailViewController ()
  12. @property (strong, nonatomic) IBOutlet FLAnimatedImageView *imageView;
  13. @property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
  14. @property (strong, nonatomic) UIProgressView *progressView;
  15. @end
  16. @implementation DetailViewController
  17. - (UIActivityIndicatorView *)activityIndicator
  18. {
  19. if (!_activityIndicator) {
  20. _activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  21. _activityIndicator.center = self.imageView.center;
  22. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  23. [self.imageView addSubview:_activityIndicator];
  24. }
  25. return _activityIndicator;
  26. }
  27. - (UIProgressView *)progressView
  28. {
  29. if (!_progressView) {
  30. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
  31. [self.view addSubview:_progressView];
  32. }
  33. return _progressView;
  34. }
  35. - (void)configureView
  36. {
  37. self.activityIndicator.hidden = NO;
  38. [self.activityIndicator startAnimating];
  39. __weak typeof(self) weakSelf = self;
  40. [self.imageView sd_setImageWithURL:self.imageURL
  41. placeholderImage:nil
  42. options:SDWebImageProgressiveDownload
  43. progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) {
  44. dispatch_async(dispatch_get_main_queue(), ^{
  45. float progress = 0;
  46. if (expectedSize != 0) {
  47. progress = (float)receivedSize / (float)expectedSize;
  48. }
  49. weakSelf.progressView.hidden = NO;
  50. [weakSelf.progressView setProgress:progress animated:YES];
  51. });
  52. }
  53. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  54. weakSelf.progressView.hidden = YES;
  55. [weakSelf.activityIndicator stopAnimating];
  56. weakSelf.activityIndicator.hidden = YES;
  57. }];
  58. }
  59. - (void)viewDidLoad
  60. {
  61. [super viewDidLoad];
  62. [self configureView];
  63. }
  64. - (void)viewDidLayoutSubviews
  65. {
  66. [super viewDidLayoutSubviews];
  67. self.progressView.frame = CGRectMake(0, self.topLayoutGuide.length, CGRectGetWidth(self.view.bounds), 2.0);
  68. }
  69. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  70. {
  71. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  72. }
  73. @end