【发布时间】: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