【发布时间】:2013-09-10 22:16:34
【问题描述】:
我正在尝试通过创建 CGBitMapContext 来创建 UICollectionViewCell 的快照。我并不完全清楚如何执行此操作或如何使用相关类,但经过一番研究,我编写了以下方法,该方法从我的 UICollectionViewCell 子类中调用:
- (void)snapShotOfCell
{
float scaleFactor = [[UIScreen mainScreen] scale];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, self.frame.size.width * scaleFactor, self.frame.size.height * scaleFactor, 8, self.frame.size.width * scaleFactor * 4, colorSpace, kCGImageAlphaPremultipliedFirst);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage *snapShot = [[UIImage alloc]initWithCGImage:image];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.frame];
imageView.image = snapShot;
imageView.opaque = YES;
[self addSubview:imageView];
CGImageRelease(image);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
}
结果是图像没有出现。调试后,我可以确定我有一个有效的(非零)上下文、CGImage、UIImage 和 UIImageView,但屏幕上什么也没有出现。谁能告诉我我错过了什么?
【问题讨论】:
标签: objective-c core-graphics cgimageref cgbitmapcontextcreate cgbitmapcontext