How to take a screenshot programmatically on iOS

前端 未结 20 2100
一生所求
一生所求 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:09

    As of iOS10, this gets a bit simpler. UIKit comes with UIGraphicsImageRender that allows you to

    ... accomplish drawing tasks, without having to handle configuration such as color depth and image scale, or manage Core Graphics contexts

    Apple Docs - UIGraphicsImageRenderer

    So you can now do something like this:

    let renderer = UIGraphicsImageRenderer(size: someView.bounds.size)
    
    let image = renderer.image(actions: { context in
        someView.drawHierarchy(in: someView.bounds, afterScreenUpdates: true)
    })
    

    Many of the answers here worked for me in most cases. But when trying to take a snapshot of an ARSCNView, I was only able to do it using the method described above. Although it might be worth noting that at this time, ARKit is still in beta and Xcode is in beta 4

提交回复
热议问题