From efb7c4ebf90b038215d2503a2b19c3dcea6f4aa5 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 5 Nov 2017 06:30:43 +0800 Subject: [PATCH] Update our iOS demo to modern way, add a UIProgressView to show image download progress --- .../SDWebImage Demo/DetailViewController.m | 83 ++++++++++--------- .../SDWebImage Demo/MasterViewController.h | 4 - .../SDWebImage Demo/MasterViewController.m | 31 +++---- .../xcschemes/SDWebImage iOS Demo.xcscheme | 4 +- 4 files changed, 57 insertions(+), 65 deletions(-) diff --git a/Examples/SDWebImage Demo/DetailViewController.m b/Examples/SDWebImage Demo/DetailViewController.m index eef1ca11..83a2d248 100644 --- a/Examples/SDWebImage Demo/DetailViewController.m +++ b/Examples/SDWebImage Demo/DetailViewController.m @@ -7,55 +7,66 @@ */ #import "DetailViewController.h" -#import +#import #import @interface DetailViewController () @property (strong, nonatomic) IBOutlet FLAnimatedImageView *imageView; - -- (void)configureView; +@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator; +@property (strong, nonatomic) UIProgressView *progressView; @end @implementation DetailViewController -@synthesize imageURL = _imageURL; -@synthesize imageView = _imageView; - -#pragma mark - Managing the detail item - -- (void)setImageURL:(NSURL *)imageURL +- (UIActivityIndicatorView *)activityIndicator { - if (_imageURL != imageURL) - { - _imageURL = imageURL; - [self configureView]; + if (!_activityIndicator) { + _activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; + _activityIndicator.center = self.imageView.center; + _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; + [self.imageView addSubview:_activityIndicator]; + } + return _activityIndicator; +} + +- (UIProgressView *)progressView +{ + if (!_progressView) { + _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; + _progressView.frame = CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), CGRectGetWidth(self.view.frame), 2.0); + _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; + [self.view addSubview:_progressView]; + } + return _progressView; } - (void)configureView { - if (self.imageURL) { - __block UIActivityIndicatorView *activityIndicator; - __weak UIImageView *weakImageView = self.imageView; - [self.imageView sd_setImageWithURL:self.imageURL - placeholderImage:nil - options:SDWebImageProgressiveDownload - progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) { - dispatch_async(dispatch_get_main_queue(), ^{ - if (!activityIndicator) { - [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]]; - activityIndicator.center = weakImageView.center; - [activityIndicator startAnimating]; - } - }); - } - completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { - [activityIndicator removeFromSuperview]; - activityIndicator = nil; - }]; - } + self.activityIndicator.hidden = NO; + [self.activityIndicator startAnimating]; + + __weak typeof(self) weakSelf = self; + [self.imageView sd_setImageWithURL:self.imageURL + placeholderImage:nil + options:SDWebImageProgressiveDownload + progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) { + dispatch_async(dispatch_get_main_queue(), ^{ + float progress = 0; + if (expectedSize != 0) { + progress = (float)receivedSize / (float)expectedSize; + } + weakSelf.progressView.hidden = NO; + [weakSelf.progressView setProgress:progress animated:YES]; + }); + } + completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { + weakSelf.progressView.hidden = YES; + [weakSelf.activityIndicator stopAnimating]; + weakSelf.activityIndicator.hidden = YES; + }]; } - (void)viewDidLoad @@ -64,12 +75,6 @@ [self configureView]; } -- (void)viewDidUnload -{ - [super viewDidUnload]; - self.imageView = nil; -} - - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); diff --git a/Examples/SDWebImage Demo/MasterViewController.h b/Examples/SDWebImage Demo/MasterViewController.h index 96983d10..cffac724 100644 --- a/Examples/SDWebImage Demo/MasterViewController.h +++ b/Examples/SDWebImage Demo/MasterViewController.h @@ -8,10 +8,6 @@ #import -@class DetailViewController; - @interface MasterViewController : UITableViewController -@property (strong, nonatomic) DetailViewController *detailViewController; - @end diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index 29c31e9d..0ec2372b 100644 --- a/Examples/SDWebImage Demo/MasterViewController.m +++ b/Examples/SDWebImage Demo/MasterViewController.m @@ -7,13 +7,10 @@ */ #import "MasterViewController.h" -#import #import "DetailViewController.h" -#import #import #import - @interface MyCustomTableViewCell : UITableViewCell @property (nonatomic, strong) UILabel *customTextLabel; @@ -21,7 +18,6 @@ @end - @implementation MyCustomTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { @@ -39,17 +35,14 @@ @end +@interface MasterViewController () +@property (nonatomic, strong) NSMutableArray *objects; -@interface MasterViewController () { - NSMutableArray *_objects; -} @end @implementation MasterViewController -@synthesize detailViewController = _detailViewController; - - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; @@ -66,7 +59,7 @@ [SDWebImageManager sharedManager].imageDownloader.username = @"httpwatch"; [SDWebImageManager sharedManager].imageDownloader.password = @"httpwatch01"; - _objects = [NSMutableArray arrayWithObjects: + self.objects = [NSMutableArray arrayWithObjects: @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633", // requires HTTP auth, used to demo the NTLM auth @"http://assets.sbnation.com/assets/2512203/dogflops.gif", @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif", @@ -80,7 +73,7 @@ nil]; for (int i=0; i<100; i++) { - [_objects addObject:[NSString stringWithFormat:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage%03d.jpg", i]]; + [self.objects addObject:[NSString stringWithFormat:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage%03d.jpg", i]]; } } @@ -109,7 +102,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return _objects.count; + return self.objects.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath @@ -130,7 +123,7 @@ [cell.customImageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray]; cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row]; - [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:_objects[indexPath.row]] + [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]] placeholderImage:placeholderImage options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; return cell; @@ -138,13 +131,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - if (!self.detailViewController) - { - self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; - } - NSString *largeImageURL = [_objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"]; - self.detailViewController.imageURL = [NSURL URLWithString:largeImageURL]; - [self.navigationController pushViewController:self.detailViewController animated:YES]; + NSString *largeImageURLString = [self.objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"]; + NSURL *largeImageURL = [NSURL URLWithString:largeImageURLString]; + DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; + detailViewController.imageURL = largeImageURL; + [self.navigationController pushViewController:detailViewController animated:YES]; } @end diff --git a/SDWebImage.xcworkspace/xcshareddata/xcschemes/SDWebImage iOS Demo.xcscheme b/SDWebImage.xcworkspace/xcshareddata/xcschemes/SDWebImage iOS Demo.xcscheme index bca49133..dae05c98 100644 --- a/SDWebImage.xcworkspace/xcshareddata/xcschemes/SDWebImage iOS Demo.xcscheme +++ b/SDWebImage.xcworkspace/xcshareddata/xcschemes/SDWebImage iOS Demo.xcscheme @@ -1,6 +1,6 @@