MasterViewController.m 6.0 KB

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