Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SDWebImage fade in UITableViewCell

I'm been using SDWebImage(ver3.0) on my iOS app, and I want to fade in the new image of uitableviewcell once it loads like Path2.0, Pinterest, and Viddy.Thanks to iOS SDWebImage fade in new image, fade-in itself is working. However, image in cell is loaded again when scrolling tableview. This may be caused by a reuse of a cell.

Here is my code.

[cell.userImageView setImageWithURL:url
                   placeholderImage:placeholderImage
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                              if (!error) {
                                  cell.userImageView.alpha = 0.0;
                                  [UIView transitionWithView:cell.userImageView
                                                    duration:1.0
                                                     options:UIViewAnimationOptionTransitionCrossDissolve
                                                  animations:^{
                                                      [cell.userImageView setImage:image];
                                                      cell.userImageView.alpha = 1.0;
                                                  } completion:NULL];
                              }
                          }];
like image 677
tsk Avatar asked Dec 25 '12 17:12

tsk


1 Answers

Credit goes to @OzBoz for pointing out that the correct solution is to just confirm whether the image was retrieved from the network (not cached) and if so, perform the animation:

[cell.userImageView setImageWithURL:url
                   placeholderImage:placeholderImage
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                              if (image && cacheType == SDImageCacheTypeNone)
                              {
                                  cell.userImageView.alpha = 0.0;
                                  [UIView animateWithDuration:1.0
                                                   animations:^{
                                                       cell.userImageView.alpha = 1.0;
                                                   }];
                              }
                          }];

If you're concerned about the cell no longer being visible), you can also use:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

This method (not to be confused by the similarly named UITableViewDataSource method) will return non-nil if the row is still visible. This is very important to do in most UITableViewCell async image retrieval processes, but it turns out that setImageWithURL will cancel any prior image retrieval processes, so it's less critical to check this when using SDImageWeb.

like image 103
Rob Avatar answered Nov 07 '22 11:11

Rob