Guest User

Untitled

a guest
Jan 17th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #import "UIImageView+AFNetworking.h"
  2.  
  3. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  4. {
  5. return posts.count;
  6. }
  7.  
  8. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  9. {
  10. static NSString *CellIdentifier = @"cell";
  11.  
  12. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  13.  
  14. if (cell == nil) {
  15. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  16. }
  17.  
  18. NSDictionary *post = [posts objectAtIndex:indexPath.row];
  19. NSString *postpictureUrl = [post objectForKey:@"picture"];
  20.  
  21. [cell.imageView setImageWithURL:[NSURL URLWithString:postpictureUrl]];
  22.  
  23. cell.textLabel.text = [post objectForKey:@"post_text"];
  24. cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];
  25. return cell;
  26. }
  27.  
  28. // ...
  29. NSDictionary *post = [posts objectAtIndex:indexPath.row];
  30. NSString *postpictureUrl = [post objectForKey:@"picture"];
  31.  
  32. // find a place in your model, or add one, to cache an actual downloaded image
  33. UIImage *postImage = [post objectForKey:@"picture_image"];
  34.  
  35. if (postImage) {
  36. cell.imageView.image = postImage; // this is the best scenario: cached image
  37. } else {
  38. // notice how we don't pass the cell - we don't trust its value past this turn of the run loop
  39. [self asynchLoad:postpictureUrl forIndexPath:indexPath];
  40. cell.imageView.image = [UIImage imageNamed:@"default"];
  41. }
  42. // ...
  43.  
  44. - (void)asynchLoad:(NSString *)urlString forIndexPath:(NSIndexPath *)indexPath {
  45.  
  46. NSURL *url = [NSURL urlWithString:urlString];
  47. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  48.  
  49. [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  50. if (!error) {
  51.  
  52. // create the image
  53. UIImage *image = [UIImage imageWithData:data];
  54.  
  55. // cache the image
  56. NSDictionary *post = [posts objectAtIndex:indexPath.row];
  57. [post setObject:image forKey:@"picture_image"];
  58.  
  59. // important part - we make no assumption about the state of the table at this point
  60. // find out if our original index path is visible, then update it, taking
  61. // advantage of the cached image (and a bonus option row animation)
  62.  
  63. NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
  64. if ([visiblePaths containsObject:indexPath]) {
  65. NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
  66. [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation: UITableViewRowAnimationFade];
  67. // because we cached the image, cellForRow... will see it and run fast
  68. }
  69. }
  70. }];
  71. }
  72.  
  73. // someplace in your code you add a post to the posts array. do this instead.
  74.  
  75. NSDictionary *postData = // however you get a new post
  76. [posts addObject:[NSMutableDictionary dictionaryWithDictionary:postData]];
  77.  
  78. @property (nonatomic,strong) NSMutableDictionary *imageCache;
  79. @synthesize imageCache=_imageCache;
  80.  
  81. // lazy init on the getter...
  82.  
  83. - (NSMutableDictionary *)imageCache {
  84. if (!_imageCache) {
  85. _imageCache = [NSMutableDictionary dictionary];
  86. }
  87. return _imageCache;
  88. }
  89.  
  90. // change to the cellForRowAtIndexPath method
  91. NSString *postpictureUrl = [post objectForKey:@"picture"];
  92. UIImage *postImage = [self.imageCache valueForKey:postpictureUrl];
  93.  
  94. // change to the asynchLoad: method I suggested
  95. UIImage *image = [UIImage imageWithData:data];
  96. [self.imageCache setValue:image forKey:urlString];
  97.  
  98. ...
  99. [cell.imageView setImageWithURL:[NSURL URLWithString:postpictureUrl] placeholderImage:[UIImage imageNamed:@"default"]];
  100. ....
Add Comment
Please, Sign In to add comment