How to take a screenshot programmatically on iOS

前端 未结 20 2485
一生所求
一生所求 2020-11-22 01:42

I want a screenshot of the image on the screen saved into the saved photo library.

20条回答
  •  情书的邮戳
    2020-11-22 02:18

    In Swift you can use following code.

    if UIScreen.mainScreen().respondsToSelector(Selector("scale")) {
        UIGraphicsBeginImageContextWithOptions(self.window!.bounds.size, false, UIScreen.mainScreen().scale)
    }
    else{
        UIGraphicsBeginImageContext(self.window!.bounds.size)
    }
    self.window?.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image : UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    

提交回复
热议问题