在uicollectionviewcell中设置为nil时,UIImageView图像未被删除

时间:2014-03-28 01:13:18

标签: ios uiimageview uiimage uicollectionview uicollectionviewcell

基本上我正在做的是拍摄视图的图像,对其应用模糊,然后在简单的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中。每当我慢慢滚动时,这很好。如果我快速滚动图像,有时只会从图像视图中删除...我不确定为什么会发生这种情况。到目前为止,我已经尝试了许多解决方案,甚至从superview中删除整个uiimageview并每次重新初始化/重新添加它作为子视图,但此操作具有相同的问题。请帮忙!

1 个答案:

答案 0 :(得分:0)

问题是可重用的集合视图单元格是......可重用的。您需要实现collectionView:cellForItemAtIndexPath:来处理所有手中的单元格,而不会对您之前是否已将子视图添加到其中做出任何假设。< / p>