如何通过代码(在 Swift 中)从我的屏幕上截取屏幕截图并保存它?我可以截图并保存为图像吗?
How can I take screenshot from my screen by code (in Swift) and save it? Can I take screenshot and save it as image?
我查看并看到了这段代码,但我无法使用它(我认为),因为它没有任何作用.
I've looked and I see this code, but I can't use it (I think) because it doesn't do anything.
var screen = UIScreen.mainScreen() snapshotVieww = screen.snapshotViewAfterScreenUpdates(false) UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0) snapshotVieww.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true) var image:UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() provino = UIImageView(image: image) 推荐答案在 Swift 4/iOS 10.3 中,您可以选择以下方式之一来解决您的问题.
With Swift 4 / iOS 10.3, you can choose one of the following ways in order to solve your problem.
以下代码展示了如何截取屏幕截图并将其保存在设备相册中:
The following code shows how to take a screenshot and save it in the device photo album:
import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage UIGraphicsBeginImageContextWithOptions(view.frame.size, true, 0) guard let context = UIGraphicsGetCurrentContext() else { return } view.layer.render(in: context) guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return } UIGraphicsEndImageContext() //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } }请注意,此代码的结果将是 .JPG 图像.还要注意导航栏和状态栏将不会出现在最终图像中.
Note that the result of this code will be a .JPG image. Also note that the navigation bar and the status bar will not appear in the final image.
从 iOS 10 开始,作为之前代码的替代,您可以使用下面的代码:
Since iOS 10, as an alternative to the previous code, you can use the code below:
import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage let renderer = UIGraphicsImageRenderer(size: view.frame.size) let image = renderer.image(actions: { context in view.layer.render(in: context.cgContext) }) //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } }2.截取 iPhone 窗口的屏幕截图
如果要截取包含导航栏(但不包括状态栏)的屏幕截图,可以使用以下代码:
2. Take a screenshot of an iPhone window
If you want to take a screenshot that includes the navigation bar (but not the status bar), you can use the following code:
import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage guard let layer = UIApplication.shared.keyWindow?.layer else { return } UIGraphicsBeginImageContextWithOptions(layer.frame.size, true, 0) guard let context = UIGraphicsGetCurrentContext() else { return } layer.render(in: context) guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return } UIGraphicsEndImageContext() //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } }从 iOS 10 开始,作为之前代码的替代,您可以使用下面的代码:
Since iOS 10, as an alternative to the previous code, you can use the code below:
import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage guard let layer = UIApplication.shared.keyWindow?.layer else { return } let renderer = UIGraphicsImageRenderer(size: layer.frame.size) let image = renderer.image(actions: { context in layer.render(in: context.cgContext) }) //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } }提醒
从 iOS 10 开始,为了防止您的应用在调用您的 screenshot(_:) 方法时崩溃,您需要将密钥 NSPhotoLibraryUsageDescription 添加到您的项目的信息中.plist 文件:
Reminder
Since iOS 10, in order to prevent your app from crashing when calling your screenshot(_:) method, you need to add the key NSPhotoLibraryUsageDescription to your project's Info.plist file:
<key>NSPhotoLibraryUsageDescription</key> <string>Some description to explain why access is required</string>更多推荐
Swift iOS 中的屏幕截图?
发布评论