【发布时间】:2014-08-18 18:29:02
【问题描述】:
我正在尝试获取 UICollectionViewCell 的快照,将其模糊并作为子视图添加到 UICollectionViewCell。
一些奇怪的问题正在发生。
MyCollectionViewController
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell<MyCollectionViewCellProtocol> *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath];
cell.modelVariable = self.modelsArray[indexPath.item];
return cell;
}
@interface MyCollectionViewCell : UICollectionViewCell <MyCollectionViewCellProtocol>
@property (strong, nonatomic) Promo *modelVariable;
@end
#import "UIImage+ImageEffects.h" // from WWDC 2013
#import "UIView+Snapshot.h" // My category to create a snapshot from a UIView
@implementation PromoRowCollectionViewCell
- (void)setModelVariable:(ModelVariableClass *)modelVariable
{
_modelVariable = modelVariable;
// just settings other stuff that is importante to the cell view
self.mainImageView.image = modelVariable.mainImage;
self.titleLabel.text = modelVariable.title;
self.shortDescriptionLabel.text = modelVariable.detailedDescription;
// removing the snapshot before taking other snapshot
// or in case the cell is reused and this modelVariable is not in BlurState
[self.blurImageView removeFromSuperview];
self.blurImageView = nil;
[self.myView removeFromSuperview];
self.myView = nil;
if (modelVariable.state == BlurState) {
// Attempt 1
// UIGraphicsBeginImageContext(self.bounds.size);
// [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
// UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// Attempt 2
// UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
// [self.layer renderInContext:UIGraphicsGetCurrentContext()];
// UIImage *imgage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// This was just a test that showed weird behavior
// UIView *snapshotView = [self snapshotViewAfterScreenUpdates:YES];
// self.myView = snapshotView;
// snapshotView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.2];
// CGRect frame = snapshotView.frame;
// frame.origin = CGPointMake(20, 20);
// snapshotView.frame = frame;
// [self addSubview:snapshotView];
// Attempt 3 // With the red background and shifting the frame we can see just a red rectangle and not the snapshot
UIView *snapshotView = [self snapshotViewAfterScreenUpdates:YES];
snapshotView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.2]; // Just to see things better
// self.blockedPromoImageView = [[UIImageView alloc] initWithImage:image]; // for attempts 1 and 2
self.blurImageView = [[UIImageView alloc] initWithImage:snapshotView.snapshotImage.applyLightEffect];
// self.blurImageView.frame = self.bounds;
self.blockedPromoImageView.frame = CGRectMake(20, 20, self.bounds.size.width, self.bounds.size.height); // Also to see things better
// self.blockedPromoImageView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.2]; // for attempts 1 and 2
[self addSubview:self.blurImageView];
}
}
@end
UIView+Snapshot
- (UIImage *)snapshotImage
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
尝试 1:
尝试 2:
以奇怪的行为进行测试:
尝试 3:
如您所见,尝试 1、2 和 3 似乎他们没有拍摄任何快照。 我希望红色矩形在后面有视图的快照。
至于奇怪的行为测试,我希望可以在图像中看到什么,除了最前面的红色矩形。
有人知道怎么回事吗?或者如何解决?
谢谢。
编辑:
我刚刚用最少的代码创建了一个简单的项目来重现问题。 https://www.dropbox.com/sh/j1mhxj367kzn81y/AADW8ds9NvLMyjUDbn08mOJXa?dl=0
【问题讨论】:
标签: ios objective-c ios7 uicollectionviewcell