gpt4 book ai didi

ios - 截取屏幕截图,然后将其保存到相机胶卷。 swift 4.2,Xcode 10

转载 作者:行者123 更新时间:2023-11-28 14:10:10 25 4
gpt4 key购买 nike

我从互联网上找到了一个源代码,可以从当前屏幕截取屏幕截图,然后将其保存到相机胶卷。但是当我点击下面的按钮时出现错误

@IBAction func btnTakeScreenShot(_ sender: UIButton) {
self.takeScreenshot()
}

open func takeScreenshot(_ shouldSave: Bool = true) -> UIImage? {
print("takeScreenshot")
var screenshotImage :UIImage?
let layer = UIApplication.shared.keyWindow!.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
guard let context = UIGraphicsGetCurrentContext() else {return nil}
layer.render(in:context)
screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let image = screenshotImage, shouldSave {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
return screenshotImage
}

错误截图如下: enter image description here

我有两个问题:

(1) 我是否应该从 Xcode 用户的build设置窗口授予任何权限以将其保存到相机胶卷?如果是的话,你能说清楚它是什么吗? (点击 HTTP 链接的权限:App Transport Security Settings, Allow Arbitrary Loads ..)

(2) 你知道为什么这个源代码会报错吗?这个源代码是否可以正常获取屏幕截图并将其保存到相机胶卷?

Swift 4.2, Xcode 10

最佳答案

您应该检查控制台是否有以下错误,

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

所以只需在 Info.plist 文件中添加 NSPhotoLibraryAddUsageDescription 键。

关于ios - 截取屏幕截图,然后将其保存到相机胶卷。 swift 4.2,Xcode 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52644022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com