swift iOS中的屏幕截图?

编程入门 行业动态 更新时间:2024-10-15 20:25:14
本文介绍了swift iOS中的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有谁知道如何通过代码(在swift中)从我的屏幕截取屏幕截图并保存? 我可以截取屏幕并将其另存为图像吗?

Does anyone know 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 = 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起,为了防止您的应用在调用屏幕截图(_ :)时崩溃方法,你需要将密钥 NSPhotoLibraryUsageDescription 添加到你的项目中nfo.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中的屏幕截图?

本文发布于:2023-11-30 03:34:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:截图   屏幕   swift   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!