iOS,iPhone快照在后台/前台转换中

时间:2013-03-14 14:38:19

标签: ios background snapshot background-foreground

当iOS应用程序将状态从前台更改为后台时,操作系统会获取当前视图(屏幕)的快照,当它再次进入前台时,它会显示该快照。 是否有任何方法(动画或任何东西)拦截该快照并在应用程序从后台到达前景时显示任何自定义图像?

注意: 我不想拍摄任何视图的快照,我只是想在应用程序从后台进入前景时显示任何自定义图像,以使其更清晰,操作系统从后台进入前景时显示的任何快照,我想要替换使用我的自定义图像的快照,我想要的行为是“每当从背景到前景发生转换时,应用程序每次都会显示我的自定义图像,而不管它在后台输入的视图。”提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您可以注册UIApplicationDidEnterBackgroundNotification通知并显示您想要呈现的图像。

答案 1 :(得分:1)

AppDelegate本身有两种委托方法:

 - applicationDidEnterBackground
 - applicationWillEnterForeground

有关此方法的详细信息,请参阅UIApplicationDelegate class reference

因此,使用这两种方法,您可以找到需要拍摄快照的事件,以及显示该图像的其他事件。

现在,如何拍摄视图的快照,所以这是一个代码片段:

    // define the frame as per your requirement
    CGRect contextRect  = CGRectMake(0, 0, myView.frame.size.width, 
                                   myView.frame.size.height); 
  // whatever you need

    UIGraphicsBeginImageContext(contextRect.size);  

    [myView.layer renderInContext:UIGraphicsGetCurrentContext()];

  UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

viewImage是包含屏幕截图图片的对象。因此,在applicationWillEnterForeground方法中以UIImageView显示此图像。

希望这能帮助您解决问题