Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "UIImageView+AFNetworking.h"
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return posts.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
- }
- NSDictionary *post = [posts objectAtIndex:indexPath.row];
- NSString *postpictureUrl = [post objectForKey:@"picture"];
- [cell.imageView setImageWithURL:[NSURL URLWithString:postpictureUrl]];
- cell.textLabel.text = [post objectForKey:@"post_text"];
- cell.detailTextLabel.text = [post objectForKey:@"post_author_name"];
- return cell;
- }
- // ...
- NSDictionary *post = [posts objectAtIndex:indexPath.row];
- NSString *postpictureUrl = [post objectForKey:@"picture"];
- // find a place in your model, or add one, to cache an actual downloaded image
- UIImage *postImage = [post objectForKey:@"picture_image"];
- if (postImage) {
- cell.imageView.image = postImage; // this is the best scenario: cached image
- } else {
- // notice how we don't pass the cell - we don't trust its value past this turn of the run loop
- [self asynchLoad:postpictureUrl forIndexPath:indexPath];
- cell.imageView.image = [UIImage imageNamed:@"default"];
- }
- // ...
- - (void)asynchLoad:(NSString *)urlString forIndexPath:(NSIndexPath *)indexPath {
- NSURL *url = [NSURL urlWithString:urlString];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
- if (!error) {
- // create the image
- UIImage *image = [UIImage imageWithData:data];
- // cache the image
- NSDictionary *post = [posts objectAtIndex:indexPath.row];
- [post setObject:image forKey:@"picture_image"];
- // important part - we make no assumption about the state of the table at this point
- // find out if our original index path is visible, then update it, taking
- // advantage of the cached image (and a bonus option row animation)
- NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
- if ([visiblePaths containsObject:indexPath]) {
- NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
- [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation: UITableViewRowAnimationFade];
- // because we cached the image, cellForRow... will see it and run fast
- }
- }
- }];
- }
- // someplace in your code you add a post to the posts array. do this instead.
- NSDictionary *postData = // however you get a new post
- [posts addObject:[NSMutableDictionary dictionaryWithDictionary:postData]];
- @property (nonatomic,strong) NSMutableDictionary *imageCache;
- @synthesize imageCache=_imageCache;
- // lazy init on the getter...
- - (NSMutableDictionary *)imageCache {
- if (!_imageCache) {
- _imageCache = [NSMutableDictionary dictionary];
- }
- return _imageCache;
- }
- // change to the cellForRowAtIndexPath method
- NSString *postpictureUrl = [post objectForKey:@"picture"];
- UIImage *postImage = [self.imageCache valueForKey:postpictureUrl];
- // change to the asynchLoad: method I suggested
- UIImage *image = [UIImage imageWithData:data];
- [self.imageCache setValue:image forKey:urlString];
- ...
- [cell.imageView setImageWithURL:[NSURL URLWithString:postpictureUrl] placeholderImage:[UIImage imageNamed:@"default"]];
- ....
Add Comment
Please, Sign In to add comment