Whenever you load from a disk, server, or render images to view a table, you will want to place it in the background. It's trivial to do and get great performance even on 3GS.
I use a similar approach to create thumbnails for table views and view scrolling from very large images, and the performance is very good.
Try the following:
- (void)configureCell:(CustomCell *)cell atIndexPath:(NSIndexPath *)indexPath { CoreDateObject *object = (CoreDateObject *)[self.fetchedResultsController objectAtIndexPath:indexPath]; cell.displayName.text = object.name; // check to see if there is a cached image already. Use a dictionary. // make sure this is one of your ivars UIImage *theImage=[self.imagesCache objectForKey: object.imagePath]; // If the image is not in your cache, you need to retrieve it. if (!theImage){ // The image doesn't exist, we need to load it from disk, web or render it // First put a placeholder image in place. Shouldn't be any penalties after the // first load because it is cached. cell.selectedImage.image=[UIImage imageNamed:@"yourPlaceHolderImage"]; // check to see if your image cache dictionary has been created, if not do so now if (_imagesCache==nil){ _imagesCache=[NSMutableDictionary alloc] initWithCapacity:1]; } // get a weak reference to UITableViewController subclass for use in the block // we do this to avoid retain cycles __weak YourTableViewControllerSubclass *weakSelf=self; // do the heavy lifting on a background queue so the UI looks fast dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async(queue, ^{ theImage=[UIImage imageWithContentOfFile:object.imagePath]; // Add the image to the cache [weakSelf.imagesCache addObject:theImage forKey:object.imagePath]; // Check to see if the cell for the specified index path is still being used CustomCell *theCell=(CustomCell *)[weakSelf.tableView cellForRowAtIndexPath:indexPath]; // Per the docs. An object representing a cell of the table // or nil if the cell is not visible or indexPath is out of range. if (theCell){ // dispatch onto the main queue because we are doing work on the UI dispatch_async(dispatch_get_main_queue(), ^{ theCell.selectedImage.image=theImage [theCell setNeedsLayout]; }); } }else{ // Image already exists, use it. cell.selectedImage.image=theImage; } cell.rating.rate = object.rating.doubleValue; }
source share