MasterViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // MasterViewController.m
  3. // SDWebImage Demo
  4. //
  5. // Created by Olivier Poitrey on 09/05/12.
  6. // Copyright (c) 2012 Dailymotion. All rights reserved.
  7. //
  8. #import "MasterViewController.h"
  9. #import <SDWebImage/UIImageView+WebCache.h>
  10. #import "DetailViewController.h"
  11. @interface MasterViewController () {
  12. NSMutableArray *_objects;
  13. }
  14. @end
  15. @implementation MasterViewController
  16. @synthesize detailViewController = _detailViewController;
  17. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  18. {
  19. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  20. if (self)
  21. {
  22. self.title = @"SDWebImage";
  23. self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Clear Cache"
  24. style:UIBarButtonItemStylePlain
  25. target:self
  26. action:@selector(flushCache)];
  27. // HTTP NTLM auth example
  28. // Add your NTLM image url to the array below and replace the credentials
  29. [SDWebImageManager sharedManager].imageDownloader.username = @"httpwatch";
  30. [SDWebImageManager sharedManager].imageDownloader.password = @"httpwatch01";
  31. _objects = [NSMutableArray arrayWithObjects:
  32. @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633", // requires HTTP auth, used to demo the NTLM auth
  33. @"http://assets.sbnation.com/assets/2512203/dogflops.gif",
  34. @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
  35. @"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
  36. nil];
  37. for (int i=0; i<100; i++) {
  38. [_objects addObject:[NSString stringWithFormat:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage%03d.jpg", i]];
  39. }
  40. }
  41. [SDWebImageManager.sharedManager.imageDownloader setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
  42. SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;
  43. return self;
  44. }
  45. - (void)flushCache
  46. {
  47. [SDWebImageManager.sharedManager.imageCache clearMemory];
  48. [SDWebImageManager.sharedManager.imageCache clearDisk];
  49. }
  50. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  51. {
  52. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  53. }
  54. #pragma mark - Table View
  55. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  56. {
  57. return 1;
  58. }
  59. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  60. {
  61. return _objects.count;
  62. }
  63. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  64. {
  65. static NSString *CellIdentifier = @"Cell";
  66. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  67. if (cell == nil)
  68. {
  69. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  70. }
  71. [cell.imageView setShowActivityIndicatorView:YES];
  72. [cell.imageView setIndicatorStyle:UIActivityIndicatorViewStyleGray];
  73. cell.textLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
  74. cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
  75. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]
  76. placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
  77. return cell;
  78. }
  79. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  80. {
  81. if (!self.detailViewController)
  82. {
  83. self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
  84. }
  85. NSString *largeImageURL = [[_objects objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];
  86. self.detailViewController.imageURL = [NSURL URLWithString:largeImageURL];
  87. [self.navigationController pushViewController:self.detailViewController animated:YES];
  88. }
  89. @end