简体   繁体   English

在Swift中,如何在没有导航栏的情况下制作快照?

[英]How do I make a snapshot without the navigation bar in iOS with Swift?

I'm using this function I've found in this site but it saves the whole screen with the navigation bar but no the status bar. 我正在使用在此站点中找到的此功能,但是它使用导航栏保存了整个屏幕,但没有保存状态栏。

func screenShotMethod() {


    let layer = UIApplication.sharedApplication().keyWindow!.layer
    let scale = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

    layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

    let alertSaved = UIAlertController(title: "Saved", message: "", preferredStyle: .ActionSheet)

    alertSaved.addAction(UIAlertAction(title: "Ok", style: .Default, handler: {action in

        self.view.removeGestureRecognizer(self.gesture)

    }))

    presentViewController(alertSaved, animated: true, completion: nil)

}

What I need is just take a snapshot from the end of the navigation bar to the bottom. 我需要的只是从导航栏的末端到底部拍摄快照。

If you change the layer you are attempting to screenshot from the keyWindow's view to your view controller's view, the navigation bar (assuming you have your view controller embedded in a navigation controller rather than adding a nav bar as a subview of your view) and the status bar will be omitted from the screenshot. 如果您要更改图层,尝试从keyWindow的视图截图到视图控制器的视图,则导航栏(假设您将视图控制器嵌入导航控制器中,而不是将导航栏添加为视图的子视图),并且屏幕快照中将省略状态栏。

let layer = view.layer

If you have blank space at the top where the navigation bar was, you can adjust the frame to cut this out. 如果导航栏位于顶部的空白处,则可以调整框架以将其切出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM