MasterViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/SDWebImageWebPCoder.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=1; i<25; i++) {
  63. // From http://r0k.us/graphics/kodak/, 768x512 resolution, 24 bit depth PNG
  64. [self.objects addObject:[NSString stringWithFormat:@"http://r0k.us/graphics/kodak/kodak/kodim%02d.png", i]];
  65. }
  66. }
  67. return self;
  68. }
  69. - (void)flushCache {
  70. [SDWebImageManager.sharedManager.imageCache clearWithCacheType:SDImageCacheTypeAll completion:nil];
  71. }
  72. #pragma mark - Table View
  73. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  74. return self.objects.count;
  75. }
  76. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  77. static NSString *CellIdentifier = @"Cell";
  78. static UIImage *placeholderImage = nil;
  79. if (!placeholderImage) {
  80. placeholderImage = [UIImage imageNamed:@"placeholder"];
  81. }
  82. MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  83. if (cell == nil) {
  84. cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  85. cell.customImageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
  86. cell.customImageView.sd_imageIndicator = SDWebImageActivityIndicator.grayIndicator;
  87. }
  88. cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
  89. [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]]
  90. placeholderImage:placeholderImage
  91. options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
  92. return cell;
  93. }
  94. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  95. NSString *largeImageURLString = [self.objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];
  96. NSURL *largeImageURL = [NSURL URLWithString:largeImageURLString];
  97. DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
  98. detailViewController.imageURL = largeImageURL;
  99. [self.navigationController pushViewController:detailViewController animated:YES];
  100. }
  101. @end