2

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 Answer 1

1

Try this instead:

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

Rendering the layer should also render all the subviews.

4
  • 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.
    – purplehey
    Feb 27, 2010 at 18:45
  • You will need to #include <QuartzCore/QuartzCore.h> Feb 27, 2010 at 19:02
  • Just remove the brackets. It is my own weird style. Feb 27, 2010 at 19:03
  • Brilliant. Thank you so much. It's confusing that the header has to be added, but the solution is exactly what I needed.
    – purplehey
    Feb 27, 2010 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.