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

只谈情不闲聊 提交于 2019-12-06 14:46:18

问题


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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/34077806/how-do-i-make-a-snapshot-without-the-navigation-bar-in-ios-with-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!