【问题标题】:Capture snapshot taken by iOS when going to background捕获iOS进入后台时拍摄的快照
【发布时间】:2016-04-27 18:31:53
【问题描述】:

每当**iOS** 应用程序进入后台时,系统都会对其进行快照以将其呈现在多任务切换器中。是否有机会访问此屏幕截图?我知道它对应于顶部窗口,这可以从

- (void)applicationDidEnterBackground:(UIApplication *)application

但是结果不是预期的,想知道iOS快照到底是什么。

【问题讨论】:

  • 您想对捕获的屏幕截图做什么?据我所知,您可以通过使用以下方法“ignoreSnapshotOnNextApplicationLaunch”来忽略 iOS 来拍摄和使用屏幕截图
  • @KetanP 我正在显示启动画面,在某些情况下,启动画面没有显示。我想知道当时正在截取什么屏幕截图。
  • 您可以通过方法'ignoreSnapshotOnNextApplicationLaunch'链接使用忽略屏幕截图:developer.apple.com/library/ios/documentation/UIKit/Reference/…

标签: ios objective-c uiview screenshot multitasking


【解决方案1】:

使用此代码捕获快照:

#import <QuartzCore/QuartzCore.h> 


if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);

[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imgData = UIImagePNGRepresentation(image);
if(imgData)
    [imgData writeToFile:@"screenshot.png" atomically:YES];
else
    NSLog(@"error while taking screenshot");

【讨论】:

  • 谢谢@prince 我应该把这段代码放在哪里?在applicationDidEnterBackground: 的末尾?
  • 您可以在 UIWindow 实例上使用较新的drawViewHierarchyInRect:afterScreenUpdates: 而不是renderInContext,也许会得到更好的结果!
  • 我得到image = nil 两种解决方案@prince @AndrewCarter
  • 你是在设备还是模拟器上测试?
【解决方案2】:

在 iOS 的 App Programming Guide 中,Apple 说:

隐藏/替换敏感信息,您可能还想通过 ignoreSnapshotOnNextApplicationLaunch 告诉 iOS 7 不要拍摄屏幕快照。根据documentation这个方法:

希望这会有所帮助。

【讨论】:

  • 我确实想截屏,所以我不想隐藏它。
猜你喜欢
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
相关资源
最近更新 更多