Knodel
Knodel

Reputation: 4389

[iOS]How to make a "snapshot" of the current view's state

I need to make a snapshot or a screenshot - call it as you like- of the current view and then display it in the modal view.

Because if I just write this in the ModalView's View controller

MyAppViewController *viewController = [[MyAppViewController alloc] init];
self.view = viewController.view;

All the methods of the MyAppViewController are called as well, but I don't need it, I just need to "save" everything that was on the screen when the ModalView appeared and show it in the ModalView's view.

How can I do it? Thanks in advance!

Upvotes: 14

Views: 12913

Answers (3)

HannahCarney
HannahCarney

Reputation: 3631

UIView *viewSnapShop = [self snapshotViewAfterScreenUpdates:NO];

Upvotes: 0

g_fred
g_fred

Reputation: 6058

In iOS 7, you can use one of the new methods in UIView for creating snapshots, for instance:

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates;

which returns a UIView which you can then add as a subview.

Upvotes: 12

Bongeh
Bongeh

Reputation: 2300

I would suggest doing this:

Have a method that creates an image out of the contents of the view.

-(UIImage*) makeImage {

UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return viewImage;
}

Create a custom init method for your modal view, and also give your modalView an instance variable that can hold a UIImage something like...

- (id)initWithImage:(UIImage *)temp {
   myImage = temp;
}

Then in your modalView, perhaps in the viewDidLoad method, create a UIImageView and set the image to myImage

Hopefully this achieves what you are trying to do.

Upvotes: 34

Related Questions