iosuilabelimage-rendering

Can renderInContext: maintain corner radius and shadows?


I have a custom UILabel with round corners and a drop shadow. I'm using the properties on CALayer to achieve this. Next, I'm trying to save this as a UIImage using renderInContext:. The round corners are maintained, but a black background appears and a loose the drop shadow.

Rendered Image

Any thoughts on rendering the UILabel as an image but maintaining the shadow and rounded corners?

Here's the code I'm using to render the label:

UIGraphicsBeginImageContextWithOptions(label.bounds.size, YES, 0);
[label.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Solution

  • You can get rounded corners by changing the second argument to "NO" in UIGraphicsBeginImageContextWithOptions.