MasterViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "MasterViewController.h"
  9. #import <SDWebImage/UIImageView+WebCache.h>
  10. #import "DetailViewController.h"
  11. #import <SDWebImage/FLAnimatedImageView.h>
  12. #import <SDWebImage/FLAnimatedImageView+WebCache.h>
  13. #import <SDWebImage/UIView+WebCache.h>
  14. @interface MyCustomTableViewCell : UITableViewCell
  15. @property (nonatomic, strong) UILabel *customTextLabel;
  16. @property (nonatomic, strong) FLAnimatedImageView *customImageView;
  17. @end
  18. @implementation MyCustomTableViewCell
  19. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  20. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  21. _customImageView = [[FLAnimatedImageView alloc] initWithFrame:CGRectMake(20.0, 2.0, 60.0, 40.0)];
  22. [self.contentView addSubview:_customImageView];
  23. _customTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 12.0, 200, 20.0)];
  24. [self.contentView addSubview:_customTextLabel];
  25. _customImageView.clipsToBounds = YES;
  26. _customImageView.contentMode = UIViewContentModeScaleAspectFill;
  27. }
  28. return self;
  29. }
  30. @end
  31. @interface MasterViewController () {
  32. NSMutableArray *_objects;
  33. }
  34. @end
  35. @implementation MasterViewController
  36. @synthesize detailViewController = _detailViewController;
  37. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  38. {
  39. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  40. if (self)
  41. {
  42. self.title = @"SDWebImage";
  43. self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Clear Cache"
  44. style:UIBarButtonItemStylePlain
  45. target:self
  46. action:@selector(flushCache)];
  47. // HTTP NTLM auth example
  48. // Add your NTLM image url to the array below and replace the credentials
  49. [SDWebImageManager sharedManager].imageDownloader.username = @"httpwatch";
  50. [SDWebImageManager sharedManager].imageDownloader.password = @"httpwatch01";
  51. _objects = [NSMutableArray arrayWithObjects:
  52. @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633", // requires HTTP auth, used to demo the NTLM auth
  53. @"http://assets.sbnation.com/assets/2512203/dogflops.gif",
  54. @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
  55. @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
  56. @"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
  57. @"http://littlesvr.ca/apng/images/SteamEngine.webp",
  58. @"http://littlesvr.ca/apng/images/world-cup-2014-42.webp",
  59. @"https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp",
  60. @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
  61. nil];
  62. for (int i=0; i<100; i++) {
  63. [_objects addObject:[NSString stringWithFormat:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage%03d.jpg", i]];
  64. }
  65. }
  66. [SDWebImageManager.sharedManager.imageDownloader setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
  67. SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;
  68. return self;
  69. }
  70. - (void)flushCache
  71. {
  72. [SDWebImageManager.sharedManager.imageCache clearMemory];
  73. [SDWebImageManager.sharedManager.imageCache clearDiskOnCompletion:nil];
  74. }
  75. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  76. {
  77. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  78. }
  79. #pragma mark - Table View
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  81. {
  82. return 1;
  83. }
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  85. {
  86. return _objects.count;
  87. }
  88. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  89. {
  90. static NSString *CellIdentifier = @"Cell";
  91. static UIImage *placeholderImage = nil;
  92. if (!placeholderImage) {
  93. placeholderImage = [UIImage imageNamed:@"placeholder"];
  94. }
  95. MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  96. if (cell == nil) {
  97. cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  98. }
  99. [cell.customImageView sd_setShowActivityIndicatorView:YES];
  100. [cell.customImageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray];
  101. cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
  102. [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:_objects[indexPath.row]]
  103. placeholderImage:placeholderImage
  104. options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
  105. return cell;
  106. }
  107. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  108. {
  109. if (!self.detailViewController)
  110. {
  111. self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
  112. }
  113. NSString *largeImageURL = [_objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];
  114. self.detailViewController.imageURL = [NSURL URLWithString:largeImageURL];
  115. [self.navigationController pushViewController:self.detailViewController animated:YES];
  116. }
  117. @end