简体   繁体   English

iOS SDWebImage淡入UITableViewCell

[英]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. 我一直在我的iOS应用程序上使用SDWebImage(ver3.0),一旦它加载像Path2.0,Pinterest和Viddy,我想淡入uitableviewcell的新图像。感谢iOS SDWebImage淡入新图像 ,淡入淡出 - 本身就是在工作。 However, image in cell is loaded again when scrolling tableview. 但是,滚动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: 感谢@OzBoz指出正确的解决方案是确认图像是否是从网络中检索的(未缓存),如果是,请执行动画:

[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. 如果行仍然可见,则此方法(不要被类似命名的UITableViewDataSource方法混淆)将返回非nil 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 . 这在大多数UITableViewCell异步图像检索过程中非常重要,但事实证明setImageWithURL将取消任何先前的图像检索过程,因此在使用SDImageWeb时检查这一点并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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