MasterViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #import <SDWebImageWebPCoder/SDImageWebPCoder.h>
  12. @interface MyCustomTableViewCell : UITableViewCell
  13. @property (nonatomic, strong) UILabel *customTextLabel;
  14. @property (nonatomic, strong) SDAnimatedImageView *customImageView;
  15. @end
  16. @implementation MyCustomTableViewCell
  17. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  18. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  19. _customImageView = [[SDAnimatedImageView 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. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  35. if (self) {
  36. self.title = @"SDWebImage";
  37. self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Clear Cache"
  38. style:UIBarButtonItemStylePlain
  39. target:self
  40. action:@selector(flushCache)];
  41. [[SDImageCodersManager sharedManager] addCoder:[SDImageWebPCoder sharedCoder]];
  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. [SDWebImageManager.sharedManager.imageCache clearWithCacheType:SDImageCacheTypeAll completion:nil];
  70. }
  71. #pragma mark - Table View
  72. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  73. return self.objects.count;
  74. }
  75. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  76. static NSString *CellIdentifier = @"Cell";
  77. static UIImage *placeholderImage = nil;
  78. if (!placeholderImage) {
  79. placeholderImage = [UIImage imageNamed:@"placeholder"];
  80. }
  81. MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  82. if (cell == nil) {
  83. cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  84. cell.customImageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
  85. cell.customImageView.sd_imageIndicator = SDWebImageActivityIndicator.grayIndicator;
  86. }
  87. cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
  88. [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]]
  89. placeholderImage:placeholderImage
  90. options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
  91. return cell;
  92. }
  93. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  94. NSString *largeImageURLString = [self.objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];
  95. NSURL *largeImageURL = [NSURL URLWithString:largeImageURLString];
  96. DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
  97. detailViewController.imageURL = largeImageURL;
  98. [self.navigationController pushViewController:detailViewController animated:YES];
  99. }
  100. @end