简体   繁体   中英

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];
                              }
                          }];

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM