MasterViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  34. if (self) {
  35. self.title = @"SDWebImage";
  36. self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Clear Cache"
  37. style:UIBarButtonItemStylePlain
  38. target:self
  39. action:@selector(flushCache)];
  40. // HTTP NTLM auth example
  41. // Add your NTLM image url to the array below and replace the credentials
  42. [SDWebImageDownloader sharedDownloader].config.username = @"httpwatch";
  43. [SDWebImageDownloader sharedDownloader].config.password = @"httpwatch01";
  44. [[SDWebImageDownloader sharedDownloader] setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
  45. [SDWebImageDownloader sharedDownloader].config.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;
  46. self.objects = [NSMutableArray arrayWithObjects:
  47. @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633", // requires HTTP auth, used to demo the NTLM auth
  48. @"http://assets.sbnation.com/assets/2512203/dogflops.gif",
  49. @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
  50. @"http://apng.onevcat.com/assets/elephant.png",
  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://nokiatech.github.io/heif/content/image_sequences/starfield_animation.heic",
  58. @"https://s2.ax1x.com/2019/11/01/KHYIgJ.gif",
  59. @"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/stack_of_photos.pdf",
  60. @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
  61. @"https://res.cloudinary.com/dwpjzbyux/raw/upload/v1666474070/RawDemo/raw_vebed5.NEF",
  62. @"https://placehold.co/200x200.jpg",
  63. nil];
  64. for (int i=1; i<25; i++) {
  65. // From http://r0k.us/graphics/kodak/, 768x512 resolution, 24 bit depth PNG
  66. [self.objects addObject:[NSString stringWithFormat:@"http://r0k.us/graphics/kodak/kodak/kodim%02d.png", i]];
  67. }
  68. }
  69. return self;
  70. }
  71. - (void)flushCache {
  72. [SDWebImageManager.sharedManager.imageCache clearWithCacheType:SDImageCacheTypeAll completion:nil];
  73. }
  74. #pragma mark - Table View
  75. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  76. return self.objects.count;
  77. }
  78. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  79. static NSString *CellIdentifier = @"Cell";
  80. static UIImage *placeholderImage = nil;
  81. if (!placeholderImage) {
  82. placeholderImage = [UIImage imageNamed:@"placeholder"];
  83. }
  84. MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  85. if (cell == nil) {
  86. cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  87. cell.customImageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
  88. cell.customImageView.sd_imageIndicator = SDWebImageActivityIndicator.grayIndicator;
  89. }
  90. cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
  91. __weak SDAnimatedImageView *imageView = cell.customImageView;
  92. [imageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]]
  93. placeholderImage:placeholderImage
  94. options:0
  95. context:@{SDWebImageContextImageThumbnailPixelSize : @(CGSizeMake(180, 120))}
  96. progress:nil
  97. completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  98. SDWebImageCombinedOperation *operation = [imageView sd_imageLoadOperationForKey:imageView.sd_latestOperationKey];
  99. SDWebImageDownloadToken *token = operation.loaderOperation;
  100. if (@available(iOS 10.0, *)) {
  101. NSURLSessionTaskMetrics *metrics = token.metrics;
  102. if (metrics) {
  103. printf("Metrics: %s download in (%f) seconds\n", [imageURL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding], metrics.taskInterval.duration);
  104. }
  105. }
  106. }];
  107. return cell;
  108. }
  109. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  110. NSString *largeImageURLString = [self.objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];
  111. NSURL *largeImageURL = [NSURL URLWithString:largeImageURLString];
  112. DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
  113. detailViewController.imageURL = largeImageURL;
  114. [self.navigationController pushViewController:detailViewController animated:YES];
  115. }
  116. @end