我正在尝试使用this代码截图:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated{
UIImageWriteToSavedPhotosAlbum([self screenshot], self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
}
- (UIImage*)screenshot
{
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[window layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
}
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}但是保存的图像的状态栏是空白的,包括信号、时间和电池。如何截取包含状态栏内容的截图?

发布于 2013-02-18 15:08:34
截图前隐藏状态栏,如下所示:
([UIApplication sharedApplication setStatusBarHidden:是];)
发布于 2014-03-20 20:03:49
(使用iOS 7和自动布局进行测试)
我使用模拟器的“截图”功能,并结合一些条件代码编译。通过取消对#define的注释,我可以快速切换到启动屏幕的“截图”模式
主要技巧是在打开屏幕的view controller出现时隐藏状态栏,但动画非常长,因此您有足够的时间点击模拟器中的屏幕截图命令,该命令会将屏幕截图直接放在桌面上(如果您愿意,也可以在设备上截图)。
使用下面的代码,状态栏立即消失,这要归功于“UIStatusBarAnimationNone”,但“滑出屏幕”是在很长一段时间内(在下面的代码中是5000.0秒)。因此,在条形图开始“移动”垂直尺寸的20个点中的1个点之前,您有大约5000/20 = 250秒的时间来截取屏幕截图(以及一些咖啡)。
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
#ifdef kTAKE_LAUNCHIMAGE_SCREENSHOT
return UIStatusBarAnimationNone;
#else
return UIStatusBarAnimationSlide;
#endif
}
- (void)removeStatusBarAnimated
{
#ifdef kTAKE_LAUNCHIMAGE_SCREENSHOT
[UIView animateWithDuration:5000.0 animations:^{
[self setNeedsStatusBarAppearanceUpdate];
}];
#else
[UIView animateWithDuration:2.0 animations:^{
[self setNeedsStatusBarAppearanceUpdate];
}];
#endif
}有关在iOS 7中控制状态栏的更多信息,请查看我的答案和代码:
发布于 2014-12-31 20:15:55
这是一个不隐藏状态栏的解决方案,只需使用正确的边界并注意y偏移量应该为负,这里的“ViewController”是在状态栏下面填充屏幕的视图:(代码写在地图中)
UIGraphicsBeginImageContextWithOptions(self.map.bounds.size, NO, [UIScreen mainScreen].scale);
CGRect bounds = CGRectMake(0, -1*(self.view.bounds.size.height-self.map.bounds.size.height), self.map.bounds.size.width, self.map.bounds.size.height);
BOOL success = [ self.view drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();https://stackoverflow.com/questions/14930519
复制相似问题