Swift iOS 中的屏幕截图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25444609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Screenshot in Swift iOS?
提问by Giorgio Nocera
How can I take screenshot from my screen by code (in Swift) and save it? Can I take screenshot and save it as image?
如何通过代码(在 Swift 中)从我的屏幕截取屏幕截图并保存它?我可以截图并保存为图像吗?
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)
回答by Imanou Petit
With Swift 4 / iOS 10.3, you can choose one of the following ways in order to solve your problem.
使用 Swift 4 / iOS 10.3,您可以选择以下方式之一来解决您的问题。
1. Take a screenshot of a view controller's view
1. 截取一个视图控制器的视图
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)
}
}
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.
请注意,此代码的结果将是 .JPG 图像。另请注意,导航栏和状态栏不会出现在最终图像中。
Since iOS 10, as an alternative to the previous code, you can use the code below:
从 iOS 10 开始,作为之前代码的替代,您可以使用以下代码:
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. Take a screenshot of an iPhone window
2. 截取 iPhone 窗口的屏幕截图
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)
}
}
Since iOS 10, as an alternative to the previous code, you can use the code below:
从 iOS 10 开始,作为之前代码的替代,您可以使用以下代码:
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)
}
}
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:
从 iOS 10 开始,为了防止你的应用在调用你的screenshot(_:)
方法时崩溃,你需要将 key 添加NSPhotoLibraryUsageDescription
到你项目的 Info.plist 文件中:
<key>NSPhotoLibraryUsageDescription</key>
<string>Some description to explain why access is required</string>
回答by Kumar KL
Just these few lines of code will get the screenShot of the View :(just Tested)
只需这几行代码即可获得视图的屏幕截图:(刚刚测试)
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imgView.image = image;
回答by Rex
// Screen shot to share on face book or twitter is like that...
// 在脸书或推特上分享的屏幕截图就是这样...
func shareButtonClickedToTwitter(){
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320,320), false, 0)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
self.view?.drawViewHierarchyInRect(CGRectMake(-30, -30, self.frame.size.width, self.frame.size.height), afterScreenUpdates: true)
var screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.vc.showTWShare("I scored \(score) in Beanystalk can you do better? Available in App Store..", shareImage: screenShot)}
func shareButtonClickedToFaceboook() {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320,320), false, 0)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
self.view?.drawViewHierarchyInRect(CGRectMake(-30, -30, self.frame.size.width, self.frame.size.height), afterScreenUpdates: true)
var screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.vc.showFbShare("I got \(score) Points whilst playing BeanyStalk! Can you beat me?..", shareImageF: screenShot) }
// function for facebook share.
// 用于 Facebook 共享的函数。
func showFbShare(messageFB: String , shareImageF: UIImage) {
println(messageFB)
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var fbSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
fbSheet.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
break
case SLComposeViewControllerResult.Done:
break
}
}
fbSheet.addImage(shareImageF)
fbSheet.setInitialText("\(messageFB) Click here to fun https://hereIsLink/")
println(messageFB)
self.presentViewController(fbSheet, animated: false, completion: {
})
}
else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
// Twitter Share function
//推特分享功能
func showTWShare(message: String , shareImage: UIImage) {
println(message)
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
var twSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
twSheet.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
break
case SLComposeViewControllerResult.Done:
break
}
}
twSheet.setInitialText("\(message) Click here 2 fun https://hereIsLink/") //The default text in the tweet
twSheet.addImage(shareImage)
println(message)
self.presentViewController(twSheet, animated: false, completion: {
})
}
else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} }
回答by C?ur
To take a screenshot, Imanou Petit answer is very good. I've dealt with the multiple versions of iOS and tvOS in a single extension, and made it available as a pod (pod 'SwiftImageEffects'
).
要截图,Imanou Petit 的回答非常好。我在一个扩展中处理了多个版本的 iOS 和 tvOS,并将其作为 pod ( pod 'SwiftImageEffects'
) 提供。
Full code below (from https://github.com/Coeur/ImageEffects/blob/master/SwiftImageEffects/ImageEffects%2Bextensions.swift):
下面的完整代码(来自https://github.com/Coeur/ImageEffects/blob/master/SwiftImageEffects/ImageEffects%2Bextensions.swift):
extension UIView {
/// Get a UIImage from the UIView
/// - parameter opaque:
/// A Boolean flag indicating whether the image is opaque. Specify true to ignore the alpha channel. Specify false to handle any partially transparent pixels.
/// - parameter scale:
/// The scale factor to apply to the image. If you specify a value of 0.0, the scale factor is set to the scale factor of the device's main screen.
open func renderImage(opaque: Bool = false, scale: CGFloat = 0) -> UIImage {
if #available(iOS 10.0, tvOS 10.0, *) {
let format = UIGraphicsImageRendererFormat.default()
format.opaque = opaque
format.scale = scale
return UIGraphicsImageRenderer(size: bounds.size, format: format).image { layer.render(in: func screenshot() {
if let image = UIApplication.shared.keyWindow?.renderImage() {
// saving will require a NSPhotoLibraryUsageDescription in your project's Info.plist
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
.cgContext) }
} else {
// Fallback on earlier versions
// The following methods will only return a 8-bit per channel context in the DeviceRGB color space.
// Any new bitmap drawing code is encouraged to use UIGraphicsImageRenderer in lieu of this API.
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, scale)
defer { UIGraphicsEndImageContext() }
layer.render(in: UIGraphicsGetCurrentContext()!)
return UIGraphicsGetImageFromCurrentImageContext()!
}
}
}
Then you may take a screenshot as simply as:
然后您可以简单地截取屏幕截图:
##代码##