Update our iOS demo to modern way, add a UIProgressView to show image download progress
Showing
4 changed files
with
45 additions
and
53 deletions
@@ -7,55 +7,66 @@ | @@ -7,55 +7,66 @@ | ||
7 | */ | 7 | */ |
8 | 8 | ||
9 | #import "DetailViewController.h" | 9 | #import "DetailViewController.h" |
10 | -#import <SDWebImage/UIImageView+WebCache.h> | 10 | +#import <SDWebImage/FLAnimatedImageView.h> |
11 | #import <SDWebImage/FLAnimatedImageView+WebCache.h> | 11 | #import <SDWebImage/FLAnimatedImageView+WebCache.h> |
12 | 12 | ||
13 | @interface DetailViewController () | 13 | @interface DetailViewController () |
14 | 14 | ||
15 | @property (strong, nonatomic) IBOutlet FLAnimatedImageView *imageView; | 15 | @property (strong, nonatomic) IBOutlet FLAnimatedImageView *imageView; |
16 | - | ||
17 | -- (void)configureView; | 16 | +@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator; |
17 | +@property (strong, nonatomic) UIProgressView *progressView; | ||
18 | 18 | ||
19 | @end | 19 | @end |
20 | 20 | ||
21 | @implementation DetailViewController | 21 | @implementation DetailViewController |
22 | 22 | ||
23 | -@synthesize imageURL = _imageURL; | ||
24 | -@synthesize imageView = _imageView; | 23 | +- (UIActivityIndicatorView *)activityIndicator |
24 | +{ | ||
25 | + if (!_activityIndicator) { | ||
26 | + _activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; | ||
27 | + _activityIndicator.center = self.imageView.center; | ||
28 | + _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; | ||
29 | + [self.imageView addSubview:_activityIndicator]; | ||
25 | 30 | ||
26 | -#pragma mark - Managing the detail item | 31 | + } |
32 | + return _activityIndicator; | ||
33 | +} | ||
27 | 34 | ||
28 | -- (void)setImageURL:(NSURL *)imageURL | 35 | +- (UIProgressView *)progressView |
29 | { | 36 | { |
30 | - if (_imageURL != imageURL) | ||
31 | - { | ||
32 | - _imageURL = imageURL; | ||
33 | - [self configureView]; | 37 | + if (!_progressView) { |
38 | + _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; | ||
39 | + _progressView.frame = CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), CGRectGetWidth(self.view.frame), 2.0); | ||
40 | + _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; | ||
41 | + [self.view addSubview:_progressView]; | ||
34 | } | 42 | } |
43 | + return _progressView; | ||
35 | } | 44 | } |
36 | 45 | ||
37 | - (void)configureView | 46 | - (void)configureView |
38 | { | 47 | { |
39 | - if (self.imageURL) { | ||
40 | - __block UIActivityIndicatorView *activityIndicator; | ||
41 | - __weak UIImageView *weakImageView = self.imageView; | 48 | + self.activityIndicator.hidden = NO; |
49 | + [self.activityIndicator startAnimating]; | ||
50 | + | ||
51 | + __weak typeof(self) weakSelf = self; | ||
42 | [self.imageView sd_setImageWithURL:self.imageURL | 52 | [self.imageView sd_setImageWithURL:self.imageURL |
43 | placeholderImage:nil | 53 | placeholderImage:nil |
44 | options:SDWebImageProgressiveDownload | 54 | options:SDWebImageProgressiveDownload |
45 | progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) { | 55 | progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) { |
46 | dispatch_async(dispatch_get_main_queue(), ^{ | 56 | dispatch_async(dispatch_get_main_queue(), ^{ |
47 | - if (!activityIndicator) { | ||
48 | - [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]]; | ||
49 | - activityIndicator.center = weakImageView.center; | ||
50 | - [activityIndicator startAnimating]; | 57 | + float progress = 0; |
58 | + if (expectedSize != 0) { | ||
59 | + progress = (float)receivedSize / (float)expectedSize; | ||
51 | } | 60 | } |
61 | + weakSelf.progressView.hidden = NO; | ||
62 | + [weakSelf.progressView setProgress:progress animated:YES]; | ||
52 | }); | 63 | }); |
53 | } | 64 | } |
54 | completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { | 65 | completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { |
55 | - [activityIndicator removeFromSuperview]; | ||
56 | - activityIndicator = nil; | 66 | + weakSelf.progressView.hidden = YES; |
67 | + [weakSelf.activityIndicator stopAnimating]; | ||
68 | + weakSelf.activityIndicator.hidden = YES; | ||
57 | }]; | 69 | }]; |
58 | - } | ||
59 | } | 70 | } |
60 | 71 | ||
61 | - (void)viewDidLoad | 72 | - (void)viewDidLoad |
@@ -64,12 +75,6 @@ | @@ -64,12 +75,6 @@ | ||
64 | [self configureView]; | 75 | [self configureView]; |
65 | } | 76 | } |
66 | 77 | ||
67 | -- (void)viewDidUnload | ||
68 | -{ | ||
69 | - [super viewDidUnload]; | ||
70 | - self.imageView = nil; | ||
71 | -} | ||
72 | - | ||
73 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | 78 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
74 | { | 79 | { |
75 | return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); | 80 | return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); |
@@ -8,10 +8,6 @@ | @@ -8,10 +8,6 @@ | ||
8 | 8 | ||
9 | #import <UIKit/UIKit.h> | 9 | #import <UIKit/UIKit.h> |
10 | 10 | ||
11 | -@class DetailViewController; | ||
12 | - | ||
13 | @interface MasterViewController : UITableViewController | 11 | @interface MasterViewController : UITableViewController |
14 | 12 | ||
15 | -@property (strong, nonatomic) DetailViewController *detailViewController; | ||
16 | - | ||
17 | @end | 13 | @end |
@@ -7,13 +7,10 @@ | @@ -7,13 +7,10 @@ | ||
7 | */ | 7 | */ |
8 | 8 | ||
9 | #import "MasterViewController.h" | 9 | #import "MasterViewController.h" |
10 | -#import <SDWebImage/UIImageView+WebCache.h> | ||
11 | #import "DetailViewController.h" | 10 | #import "DetailViewController.h" |
12 | -#import <SDWebImage/FLAnimatedImageView.h> | ||
13 | #import <SDWebImage/FLAnimatedImageView+WebCache.h> | 11 | #import <SDWebImage/FLAnimatedImageView+WebCache.h> |
14 | #import <SDWebImage/UIView+WebCache.h> | 12 | #import <SDWebImage/UIView+WebCache.h> |
15 | 13 | ||
16 | - | ||
17 | @interface MyCustomTableViewCell : UITableViewCell | 14 | @interface MyCustomTableViewCell : UITableViewCell |
18 | 15 | ||
19 | @property (nonatomic, strong) UILabel *customTextLabel; | 16 | @property (nonatomic, strong) UILabel *customTextLabel; |
@@ -21,7 +18,6 @@ | @@ -21,7 +18,6 @@ | ||
21 | 18 | ||
22 | @end | 19 | @end |
23 | 20 | ||
24 | - | ||
25 | @implementation MyCustomTableViewCell | 21 | @implementation MyCustomTableViewCell |
26 | 22 | ||
27 | - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { | 23 | - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
@@ -39,17 +35,14 @@ | @@ -39,17 +35,14 @@ | ||
39 | 35 | ||
40 | @end | 36 | @end |
41 | 37 | ||
38 | +@interface MasterViewController () | ||
42 | 39 | ||
40 | +@property (nonatomic, strong) NSMutableArray<NSString *> *objects; | ||
43 | 41 | ||
44 | -@interface MasterViewController () { | ||
45 | - NSMutableArray *_objects; | ||
46 | -} | ||
47 | @end | 42 | @end |
48 | 43 | ||
49 | @implementation MasterViewController | 44 | @implementation MasterViewController |
50 | 45 | ||
51 | -@synthesize detailViewController = _detailViewController; | ||
52 | - | ||
53 | - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | 46 | - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
54 | { | 47 | { |
55 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | 48 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
@@ -66,7 +59,7 @@ | @@ -66,7 +59,7 @@ | ||
66 | [SDWebImageManager sharedManager].imageDownloader.username = @"httpwatch"; | 59 | [SDWebImageManager sharedManager].imageDownloader.username = @"httpwatch"; |
67 | [SDWebImageManager sharedManager].imageDownloader.password = @"httpwatch01"; | 60 | [SDWebImageManager sharedManager].imageDownloader.password = @"httpwatch01"; |
68 | 61 | ||
69 | - _objects = [NSMutableArray arrayWithObjects: | 62 | + self.objects = [NSMutableArray arrayWithObjects: |
70 | @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633", // requires HTTP auth, used to demo the NTLM auth | 63 | @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633", // requires HTTP auth, used to demo the NTLM auth |
71 | @"http://assets.sbnation.com/assets/2512203/dogflops.gif", | 64 | @"http://assets.sbnation.com/assets/2512203/dogflops.gif", |
72 | @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif", | 65 | @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif", |
@@ -80,7 +73,7 @@ | @@ -80,7 +73,7 @@ | ||
80 | nil]; | 73 | nil]; |
81 | 74 | ||
82 | for (int i=0; i<100; i++) { | 75 | for (int i=0; i<100; i++) { |
83 | - [_objects addObject:[NSString stringWithFormat:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage%03d.jpg", i]]; | 76 | + [self.objects addObject:[NSString stringWithFormat:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage%03d.jpg", i]]; |
84 | } | 77 | } |
85 | 78 | ||
86 | } | 79 | } |
@@ -109,7 +102,7 @@ | @@ -109,7 +102,7 @@ | ||
109 | 102 | ||
110 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | 103 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
111 | { | 104 | { |
112 | - return _objects.count; | 105 | + return self.objects.count; |
113 | } | 106 | } |
114 | 107 | ||
115 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | 108 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
@@ -130,7 +123,7 @@ | @@ -130,7 +123,7 @@ | ||
130 | [cell.customImageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray]; | 123 | [cell.customImageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray]; |
131 | 124 | ||
132 | cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row]; | 125 | cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row]; |
133 | - [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:_objects[indexPath.row]] | 126 | + [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]] |
134 | placeholderImage:placeholderImage | 127 | placeholderImage:placeholderImage |
135 | options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; | 128 | options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; |
136 | return cell; | 129 | return cell; |
@@ -138,13 +131,11 @@ | @@ -138,13 +131,11 @@ | ||
138 | 131 | ||
139 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | 132 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
140 | { | 133 | { |
141 | - if (!self.detailViewController) | ||
142 | - { | ||
143 | - self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; | ||
144 | - } | ||
145 | - NSString *largeImageURL = [_objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"]; | ||
146 | - self.detailViewController.imageURL = [NSURL URLWithString:largeImageURL]; | ||
147 | - [self.navigationController pushViewController:self.detailViewController animated:YES]; | 134 | + NSString *largeImageURLString = [self.objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"]; |
135 | + NSURL *largeImageURL = [NSURL URLWithString:largeImageURLString]; | ||
136 | + DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; | ||
137 | + detailViewController.imageURL = largeImageURL; | ||
138 | + [self.navigationController pushViewController:detailViewController animated:YES]; | ||
148 | } | 139 | } |
149 | 140 | ||
150 | @end | 141 | @end |
1 | <?xml version="1.0" encoding="UTF-8"?> | 1 | <?xml version="1.0" encoding="UTF-8"?> |
2 | <Scheme | 2 | <Scheme |
3 | - LastUpgradeVersion = "0900" | 3 | + LastUpgradeVersion = "0910" |
4 | version = "1.3"> | 4 | version = "1.3"> |
5 | <BuildAction | 5 | <BuildAction |
6 | parallelizeBuildables = "YES" | 6 | parallelizeBuildables = "YES" |
@@ -66,7 +66,7 @@ | @@ -66,7 +66,7 @@ | ||
66 | <EnvironmentVariables> | 66 | <EnvironmentVariables> |
67 | <EnvironmentVariable | 67 | <EnvironmentVariable |
68 | key = "OS_ACTIVITY_MODE" | 68 | key = "OS_ACTIVITY_MODE" |
69 | - value = "disable" | 69 | + value = "default" |
70 | isEnabled = "YES"> | 70 | isEnabled = "YES"> |
71 | </EnvironmentVariable> | 71 | </EnvironmentVariable> |
72 | </EnvironmentVariables> | 72 | </EnvironmentVariables> |
-
Please register or login to post a comment