views:

267

answers:

1

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.

+1  A: 

Try this instead:

UIGraphicsBeginImageContext(kSBCanvas.bounds.size);
{
    [kSBCanvas.layer renderInContext: UIGraphicsGetCurrentContext()];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();

Rendering the layer should also render all the subviews.

St3fan
Seems right, but the compiler says "no 'renderInContext:' method found? kSBCanvas is absolutely an object that's a subclass of UIImageView. I know this is simple. What's going on? Also, I can't use theImage if it's bracked as shown above. No big deal.
heymon
You will need to `#include <QuartzCore/QuartzCore.h>`
St3fan
Just remove the brackets. It is my own weird style.
St3fan
Brilliant. Thank you so much. It's confusing that the header has to be added, but the solution is exactly what I needed.
heymon