【问题标题】:UIImageView image not being removed when set to nil in uicollectionviewcell在 uicollectionviewcell 中设置为 nil 时,UIImageView 图像不会被删除
【发布时间】:2014-03-28 01:13:17
【问题描述】:

基本上我正在做的是获取视图的图像,对其应用模糊,然后将其用作简单 uicollectionview 中可重用集合视图单元格中的模糊 uiview 覆盖。

// Capture Screen for blurr.
-(UIImage *) captureScreen:(CGRect)frame
{
    CGRect grabRect = frame;

    //for retina displays
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    {
        UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale);
    }
    else
    {
        UIGraphicsBeginImageContext(grabRect.size);
    }

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
    [self.contentView.layer renderInContext:ctx];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    viewImage = [viewImage applyBlurWithRadius:1.8f tintColor:nil saturationDeltaFactor:1.0 maskImage:viewImage atFrame:self.imageView.frame];

    return viewImage;
}

// Set blurred image as image view image.
- (void) updateBlur
{
    UIImage* infoViewImage = [self captureScreen:self.infoView.frame];
    self.infoImageView.image = infoViewImage;
}

// Prepare for reuse.
- (void) prepareForReuse
{
    self.infoImageView.image = nil;

}

注意 uiimageview 是在初始化时创建并作为子视图添加到单元格的 contentView 中的。每当我慢慢滚动时,它都可以正常工作。如果我快速滚动,图像有时只会从图像视图中删除......我不确定为什么会发生这种情况。到目前为止,我已经尝试了许多解决方案,甚至从超级视图中删除整个 uiimageview 并每次将其重新初始化/重新添加为子视图,但此操作具有相同的问题。请帮忙!

【问题讨论】:

  • 您是如何确定图像有时只是从视图中删除的? prepareForReuse 方法仅在从cellForRowAtIndexPath 中的重用队列中取出一个单元格时调用,并且您可能会在此时将图像替换为另一个图像。
  • 我可以通过观察应用程序来判断。还有另一个图像视图,后面有另一个图像。有时,最有可能出现在集合视图单元格中的图像最终会出现在我的叠加图像视图中。
  • 但你不是一直在 cellForRowAtIndexPath 中调用 updateBlur 吗?我的观点是,如果您随后在 cellForRowAtIndexPath 中设置 .image = infoViewImage,则设置 .image = nil 无效。另一方面,如果你确实设置了.image = nil,但没有设置.image = infoViewImage,那怎么办?
  • 图像从 cellforindexpath 中的控制器异步获取。
  • 我明白了,那是完全不同的蠕虫罐头。在不了解更多关于您的单元格结构以及 cellForItemAtIndexPath 如何配置单元格的情况下,我认为我无法提供太多帮助。

标签: ios uiimageview uiimage uicollectionview uicollectionviewcell


【解决方案1】:

问题在于可重复使用的集合视图单元格是......可重复使用的。您需要实现collectionView:cellForItemAtIndexPath: 来处理它曾经 提交的每个 单元格,而无需对您之前是否可能已将子视图添加到其中做出任何假设。

【讨论】:

  • 我问的不是关于如何设置集合视图的问题。我要求您做出的假设是它们被设置为可重用的集合视图单元格。在这种情况下,我们正在查看具有 UIImageView 的 UICollectionViewCell,该 UIImageView 在重新使用时设置图像,然后在准备重新使用时删除该图像。问题是,在 uicollectionviewcell 提供的准备重用功能中快速滚动时,图像并不总是被删除。
  • 是的,我建议prepareForReuse 可能不是执行此操作的正确位置,您应该尝试在collectionView:cellForItemAtIndexPath: 中执行此操作。至少试一试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
相关资源
最近更新 更多