在 ios7 中拍摄状态栏的快照



我正在开发一个cocos2d游戏,我需要拍摄状态栏的快照。我发现ios7通过以下语句支持全屏视图捕获(包括状态栏):

UIView *screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];

我将此视图转换为UIImage,然后使用"UIImageWriteToSavedPhotosAlbum"保存它。但是,保存的屏幕截图都是白色的。我该如何解决这个问题?

它说,基于苹果文档,该方法从渲染服务器捕获屏幕的当前视觉内容,并使用它们来构建新的快照视图,如果任何屏幕视图尚未提交到渲染服务器,快照的该部分将没有内容。所以这可能是一个问题。

如果可以,您可以使用

UIView *screenshotView = [[self view] snapshotViewAfterScreenUpdates:NO];

或者还有其他方法,请参阅以下文档 https://developer.apple.com/library/ios/qa/qa1703/_index.html

UIView *screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]; 

将返回无法转换为图像的 UIReplicateView。您必须使用

drawViewHierarchyInRect:afterScreenUpdates:

这样~

    - (UIImage *)snapshot:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

它都在苹果文档中https://developer.apple.com/library/ios/qa/qa1817/_index.html

最新更新