Update our iOS demo to modern way, add a UIProgressView to show image download progress

This commit is contained in:
DreamPiggy 2017-11-05 06:30:43 +08:00
parent 821c2f3b8a
commit efb7c4ebf9
4 changed files with 57 additions and 65 deletions

View File

@ -7,55 +7,66 @@
*/ */
#import "DetailViewController.h" #import "DetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h> #import <SDWebImage/FLAnimatedImageView.h>
#import <SDWebImage/FLAnimatedImageView+WebCache.h> #import <SDWebImage/FLAnimatedImageView+WebCache.h>
@interface DetailViewController () @interface DetailViewController ()
@property (strong, nonatomic) IBOutlet FLAnimatedImageView *imageView; @property (strong, nonatomic) IBOutlet FLAnimatedImageView *imageView;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
- (void)configureView; @property (strong, nonatomic) UIProgressView *progressView;
@end @end
@implementation DetailViewController @implementation DetailViewController
@synthesize imageURL = _imageURL; - (UIActivityIndicatorView *)activityIndicator
@synthesize imageView = _imageView;
#pragma mark - Managing the detail item
- (void)setImageURL:(NSURL *)imageURL
{ {
if (_imageURL != imageURL) if (!_activityIndicator) {
{ _activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_imageURL = imageURL; _activityIndicator.center = self.imageView.center;
[self configureView]; _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 - (void)configureView
{ {
if (self.imageURL) { self.activityIndicator.hidden = NO;
__block UIActivityIndicatorView *activityIndicator; [self.activityIndicator startAnimating];
__weak UIImageView *weakImageView = self.imageView;
[self.imageView sd_setImageWithURL:self.imageURL __weak typeof(self) weakSelf = self;
placeholderImage:nil [self.imageView sd_setImageWithURL:self.imageURL
options:SDWebImageProgressiveDownload placeholderImage:nil
progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) { options:SDWebImageProgressiveDownload
dispatch_async(dispatch_get_main_queue(), ^{ progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) {
if (!activityIndicator) { dispatch_async(dispatch_get_main_queue(), ^{
[weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]]; float progress = 0;
activityIndicator.center = weakImageView.center; if (expectedSize != 0) {
[activityIndicator startAnimating]; progress = (float)receivedSize / (float)expectedSize;
} }
}); weakSelf.progressView.hidden = NO;
} [weakSelf.progressView setProgress:progress animated:YES];
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { });
[activityIndicator removeFromSuperview]; }
activityIndicator = nil; completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}]; weakSelf.progressView.hidden = YES;
} [weakSelf.activityIndicator stopAnimating];
weakSelf.activityIndicator.hidden = YES;
}];
} }
- (void)viewDidLoad - (void)viewDidLoad
@ -64,12 +75,6 @@
[self configureView]; [self configureView];
} }
- (void)viewDidUnload
{
[super viewDidUnload];
self.imageView = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

View File

@ -8,10 +8,6 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@class DetailViewController;
@interface MasterViewController : UITableViewController @interface MasterViewController : UITableViewController
@property (strong, nonatomic) DetailViewController *detailViewController;
@end @end

View File

@ -7,13 +7,10 @@
*/ */
#import "MasterViewController.h" #import "MasterViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "DetailViewController.h" #import "DetailViewController.h"
#import <SDWebImage/FLAnimatedImageView.h>
#import <SDWebImage/FLAnimatedImageView+WebCache.h> #import <SDWebImage/FLAnimatedImageView+WebCache.h>
#import <SDWebImage/UIView+WebCache.h> #import <SDWebImage/UIView+WebCache.h>
@interface MyCustomTableViewCell : UITableViewCell @interface MyCustomTableViewCell : UITableViewCell
@property (nonatomic, strong) UILabel *customTextLabel; @property (nonatomic, strong) UILabel *customTextLabel;
@ -21,7 +18,6 @@
@end @end
@implementation MyCustomTableViewCell @implementation MyCustomTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
@ -39,17 +35,14 @@
@end @end
@interface MasterViewController ()
@property (nonatomic, strong) NSMutableArray<NSString *> *objects;
@interface MasterViewController () {
NSMutableArray *_objects;
}
@end @end
@implementation MasterViewController @implementation MasterViewController
@synthesize detailViewController = _detailViewController;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{ {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
@ -66,7 +59,7 @@
[SDWebImageManager sharedManager].imageDownloader.username = @"httpwatch"; [SDWebImageManager sharedManager].imageDownloader.username = @"httpwatch";
[SDWebImageManager sharedManager].imageDownloader.password = @"httpwatch01"; [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://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", @"http://assets.sbnation.com/assets/2512203/dogflops.gif",
@"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif", @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
@ -80,7 +73,7 @@
nil]; nil];
for (int i=0; i<100; i++) { 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 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ {
return _objects.count; return self.objects.count;
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
@ -130,7 +123,7 @@
[cell.customImageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray]; [cell.customImageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row]; 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 placeholderImage:placeholderImage
options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
return cell; return cell;
@ -138,13 +131,11 @@
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ {
if (!self.detailViewController) NSString *largeImageURLString = [self.objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];
{ NSURL *largeImageURL = [NSURL URLWithString:largeImageURLString];
self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
} detailViewController.imageURL = largeImageURL;
NSString *largeImageURL = [_objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"]; [self.navigationController pushViewController:detailViewController animated:YES];
self.detailViewController.imageURL = [NSURL URLWithString:largeImageURL];
[self.navigationController pushViewController:self.detailViewController animated:YES];
} }
@end @end

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Scheme <Scheme
LastUpgradeVersion = "0900" LastUpgradeVersion = "0910"
version = "1.3"> version = "1.3">
<BuildAction <BuildAction
parallelizeBuildables = "YES" parallelizeBuildables = "YES"
@ -66,7 +66,7 @@
<EnvironmentVariables> <EnvironmentVariables>
<EnvironmentVariable <EnvironmentVariable
key = "OS_ACTIVITY_MODE" key = "OS_ACTIVITY_MODE"
value = "disable" value = "default"
isEnabled = "YES"> isEnabled = "YES">
</EnvironmentVariable> </EnvironmentVariable>
</EnvironmentVariables> </EnvironmentVariables>