Brett Brett - 1 year ago 210
Objective-C Question

UIImage from CALayer - iPhone SDK

In my application, I have created a

(with a few sublayers - the CALayer is composed of shapes added as sublayers).

I am trying to create a
that I will be able to upload to a server (I have the code for this).
However, I can't figure out how to add the
to a

Is this possible? Any advice would be excellent and very appreciated.

Many thanks,

Answer Source

Sounds like you want to render your layer into a UIImage. In that case, the method below should do the trick. Just add this to your view or controller class, or create a category on CALayer.

- (UIImage *)imageFromLayer:(CALayer *)layer
  UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, 0);

  [layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();


  return outputImage;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download