向UICollectionViewCell添加阴影会减慢滚动速度

时间:2013-01-03 23:00:35

标签: objective-c uicollectionviewcell

我可以通过添加以下代码成功地将阴影添加到UICollectionViewCell cellForItemAtIndexPath方法

cell.image.layer.masksToBounds = NO;
cell.image.moviePlayerView.layer.shadowColor = [UIColor darkGrayColor].CGColor;
cell.image.moviePlayerView.self.layer.shadowOffset = CGSizeMake(0, 5);
cell.image.moviePlayerView.self.layer.shadowRadius = 2;
cell.image.moviePlayerView.layer.shadowOpacity = 0.3;

但它使滚动速度变慢。我理解它在滚动时向单元格添加阴影但无法找出解决方案。 :/

1 个答案:

答案 0 :(得分:0)

我找到了解决方案并希望这对其他人有所帮助。现在我将现有图像与阴影图像合并并使用结果图像,而不是单独添加阴影。

UIImage* image = [image imageWithFileShadow];

- (UIImage *)imageWithFileShadow {
static CGFloat pointScale = -1.0f;
if (pointScale < 0.0f) {
    pointScale = [[UIScreen mainScreen] scale];
}

CGFloat imageWidth = (int)self.size.width;
CGFloat imageHeight = (int)self.size.height;

UIImage * shadowImage = [[UIImage imageNamed:@"FileShadowResizable"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.0f, 24.0f, 29.0f, 24.0f)];
UIGraphicsBeginImageContextWithOptions(self.size, NO, pointScale);
[shadowImage drawInRect:CGRectMake(0.0f, 0.0f, imageWidth, imageHeight) blendMode:kCGBlendModeNormal alpha:1.0f];
[self drawInRect:CGRectMake(14.0f, 8.0f, self.size.width - 28.0f, self.size.height - 29.0f)];

UIImage * mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return mergedImage;
}