Last Updated: October 04, 2020
·
20.71K
· cybersamx

Take a snapshot of a UIView as a UIImage

Problem

You want to take a snapshot of a UIView as a UIImage.

Solution

It's sometimes useful to create a UIImageView representing a visual snapshot of another UIView object. This new UIImageView can be useful for animation.

UIView *subView = self.viewWithManySubViews;
UIGraphicsBeginImageContextWithOptions(subView.bounds.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[subView.layer renderInContext:context];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *snapshotImageView = [[UIImageView alloc] initWithImage:snapshotImage];

1 Response
Add your response

Is this solution in Swift as well?

over 1 year ago ·