[iOS]如何制作“快照”当前视图的状态 [英] [iOS]How to make a "snapshot" of the current view's state

查看:106
本文介绍了[iOS]如何制作“快照”当前视图的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作快照或截图 - 根据需要调用当前视图,然后在模态视图中显示它。

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.

因为如果我只是在ModalView的View控制器中写这个

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

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

MyAppViewController的所有方法也被调用,但我不需要它,我只是当ModalView出现时,需要保存屏幕上的所有内容,并在ModalView的视图中显示它。

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!

推荐答案

我建议这样做:

有一个方法可以从视图内容中创建一个图像。

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;
}

为模态视图创建自定义初始化方法,并为modalView提供一个可以保存UIImage的实例变量... ...

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;
}

然后在你的modalView中,也许在viewDidLoad方法中,创建一个UIImageView并设置图像为 myImage

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.

这篇关于[iOS]如何制作“快照”当前视图的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆