iphone - iOS SDWebImage 在 UITableViewCell 中淡入淡出

标签 iphone ios uitableview fadein sdwebimage

我一直在我的 iOS 应用程序上使用 SDWebImage(ver3.0),我想在它像 Path2.0、Pinterest 和 Viddy 一样加载后淡入 uitableviewcell 的新图像。感谢 iOS SDWebImage fade in new image ,淡入本身正在工作。 但是,当滚动 tableview 时,单元格中的图像会再次加载。 这可能是由单元格的重用引起的。

这是我的代码。

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

最佳答案

感谢@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;
                                                   }];
                              }
                          }];

如果您担心单元格不再可见),您还可以使用:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

如果该行仍然可见,此方法(不要与名称相似的 UITableViewDataSource 方法混淆)将返回非 nil。这在大多数 UITableViewCell 异步图像检索过程中非常重要,但事实证明 setImageWithURL 将取消任何先前的图像检索过程,因此在以下情况下检查它不太重要使用 SDImageWeb

关于iphone - iOS SDWebImage 在 UITableViewCell 中淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032818/

相关文章:

iphone - 向几个 UIView 添加一个手势识别器

ios - 如何在 Swift 中仅显示 Web View 的选定部分

swift - 在表格 View 单元格中键入时键盘会覆盖 TextView

ios - tableview 自定义单元格 : use of undeclared type

iphone - UItableView删除行使用函数

ios - NSJSONSerialization 不更新(可能被缓存)

ios - 如何使用 Facebook SDK 在我的应用程序中注销 facebook

iOS:使用凭据快速创建结构

objective-c - 向下滚动时在表格 View 上附加数据

iphone - 以编程方式打开/关闭 GPS,而不提示 iPhone 中的用户