I am trying to create an image of a UIView
with CALayer
sublayers. With this code
- (UIImage*)snapshot{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
I get an image, but it doesn't accurately represent what is displayed on the screen. In particular, it does not respect the CAShapeLayer
mask property. My rounded corners don't appear in the image.
Here is a comparison of how an example appears on the screen to how it appears in the image:
Just to clarify, the image on the right is placed in the same position as the old object, basically rasterizing the object. That is why there is the same background; it is not part of the object that I'm capturing the image of.
How can I capture an image of the object while retaining the CAShapeLayer
mask in the image?
EDIT: It seems it was the layer mask that was the problem, not the path.