MasterViewController.m 6.1 KB

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