[英]How do I force a UIImageView and its subviews to redraw in a new context?
kSBCanvas is a SBCanvas, which is a subclass of UIImageView. kSBCanvas是SBCanvas,它是UIImageView的子类。 It has a few UIImageView subviews. 它具有几个UIImageView子视图。 It all renders great to the iPhone screen. 所有这些都为iPhone屏幕带来了绝佳的呈现效果。
I need composite the kSBCanvas and its subviews to an imageview that I want to write to disk. 我需要将kSBCanvas及其子视图合成到要写入磁盘的imageview。
I do the following: 我执行以下操作:
UIGraphicsBeginImageContext(kSBCanvas.bounds.size);
[kSBCanvas drawRect: [kSBCanvas bounds]];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
then get a PNG representation and write it to disk. 然后获取一个PNG表示形式并将其写入磁盘。
The kSBCanvas renders, but not the subview images. kSBCanvas呈现但不呈现子视图图像。 I checked, and kBCanvas has subviews. 我检查了一下,kBCanvas有子视图。 Do I have to call drawRect on the subviews explicitly? 我是否必须在子视图上显式调用drawRect? Easy enough, but it does not seem right. 足够容易,但是看起来并不正确。
Try this instead: 尝试以下方法:
UIGraphicsBeginImageContext(kSBCanvas.bounds.size);
{
[kSBCanvas.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
Rendering the layer should also render all the subviews. 渲染层也应该渲染所有子视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.