Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 3.01 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Best way to cache images on ios app?
  2. @interface FirstViewController : UITableViewController{
  3.  
  4. /**/
  5. NSCache *_imageCache;
  6. }
  7.        
  8. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  9. {
  10. static NSString *CellIdentifier = @"hallo";
  11. CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  12.  
  13. if (cell == nil)
  14. {
  15.     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  16. }
  17.  
  18. NSMutableArray *marr = [hallo objectAtIndex:indexPath.section];
  19. NSDictionary *dict = [marr objectAtIndex:indexPath.row];
  20.  
  21. NSString* imageName = [dict objectForKey:@"Image"];
  22. //NSLog(@"url: %@", imageURL);
  23.  
  24. UIImage *image = [_imageCache objectForKey:imageName];
  25.  
  26. if(image)
  27. {
  28.     cell.imageView.image = image;
  29. }
  30. else
  31. {
  32.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  33.  
  34.         NSString* imageURLString = [NSString stringWithFormat:@"example.com/%@", imageName];
  35.         NSURL *imageURL = [NSURL URLWithString:imageURLString];
  36.         UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];
  37.  
  38.         if(image)
  39.         {
  40.             dispatch_async(dispatch_get_main_queue(), ^{
  41.                 CustomCell *cell =(CustomCell*)[self.tableView cellForRowAtIndexPath:indexPath];
  42.                 if(cell)
  43.                 {
  44.                     cell.imageView.image = image;
  45.                 }
  46.             });
  47.             [_imageCache setObject:image forKey:imageName];
  48.         }
  49.     });
  50. }
  51.  
  52. cell.textLabel.text = [dict objectForKey:@"Name"];
  53.  
  54. return cell;
  55. }
  56.        
  57. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  58. {
  59.     static NSString *CellIdentifier = @"ilvcCell";
  60.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  61.  
  62.     // set the various cell properties
  63.  
  64.     // now update the cell image
  65.  
  66.     NSString *imagename = [self imageFilename:indexPath]; // the name of the image being retrieved
  67.  
  68.     UIImage *image = [_imageCache objectForKey:imagename];
  69.  
  70.     if (image)
  71.     {
  72.         // if we have an cachedImage sitting in memory already, then use it
  73.  
  74.         cell.imageView.image = image;
  75.     }
  76.     else
  77.     {
  78.         cell.imageView.image = [UIView imageNamed:@"blank_cell_image.png"];
  79.  
  80.         // the get the image in the background
  81.  
  82.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  83.  
  84.             // get the UIImage
  85.  
  86.             UIImage *image = [self getImage:imagename];
  87.  
  88.             // if we found it, then update UI
  89.  
  90.             if (image)
  91.             {
  92.                 dispatch_async(dispatch_get_main_queue(), ^{
  93.  
  94.                     // if the cell is visible, then set the image
  95.  
  96.                     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  97.                     if (cell)
  98.                         cell.imageView.image = image;
  99.                 });
  100.  
  101.                 [_imageCache setObject:image forKey:imagename];
  102.             }
  103.         });
  104.     }
  105.  
  106.     return cell;
  107. }