将UIView及其所有子视图绘制到Context

时间:2011-01-28 08:49:09

标签: ios ios4

创建一个UIView并保持小snow.png添加到UIView并模拟下雪效果。

然后我想将屏幕捕捉为图像。

UIGraphicsBeginImageContext(self.uiviewPreview.bounds.size);
[self.uiviewPreview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

注意:self.uiviewPreview是UIView。

但我只能在保存的jpg中看到UIview中的图像。但是当时所有添加的子视图都没有保存。

我想知道renderInContext是否正确使用

1 个答案:

答案 0 :(得分:1)

这是正确的方法,它应该有效。

请注意,该方法有一个很大的缺陷,它不能反映图层动画的实际状态(alpha,autoresizing,repositioning等)。如果您在动画时绘制图层,则无法使用。