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. It has a few UIImageView subviews. It all renders great to the iPhone screen.
I need composite the kSBCanvas and its subviews to an imageview that I want to write to disk.
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.
The kSBCanvas renders, but not the subview images. I checked, and kBCanvas has subviews. Do I have to call drawRect on the subviews explicitly? 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.
精彩评论