jdwyta
Last Updated: July 10, 2016
·
10.53K
· cybersamx
Portrait sam

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];
Say Thanks
Respond

1 Response
Add your response

27851

Is this solution in Swift as well?

11 months ago ·