开发者

How do I force a UIImageView and its subviews to redraw in a new context?

开发者 https://www.devze.com 2022-12-21 22:15 出处:网络
kSBCanvas is a SBCanvas, which is a subclass of UIImageView.It has a few UIImageView subviews.It all renders great to the iPhone screen.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消