gpt4 book ai didi

ios - Firebase 存储下载到本地文件错误

转载 作者:可可西里 更新时间:2023-10-31 23:57:50 26 4
gpt4 key购买 nike

我正在尝试从我的 Firebase 存储中下载图像 f5bd8360.jpeg
当我使用 dataWithMaxSize:completion 将此图像下载到内存时,我可以下载了。

当我尝试使用 writeToFile: 实例方法将图像下载到本地文件时出现问题。

我收到以下错误:

Optional(Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown
error occurred, please check the server response."
UserInfo={object=images/f5bd8360.jpeg,
bucket=fir-test-3d9a6.appspot.com, NSLocalizedDescription=An unknown
error occurred, please check the server response.,
ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/Documents/images,
NSUnderlyingError=0x1700562c0 {Error Domain=NSPOSIXErrorDomain Code=1
"Operation not permitted"}, ResponseErrorCode=513}"

这是我的 Swift 代码片段:

@IBAction func buttonClicked(_ sender: UIButton) {

// Get a reference to the storage service, using the default Firebase App
let storage = FIRStorage.storage()

// Get reference to the image on Firebase Storage
let imageRef = storage.reference(forURL: "gs://fir-test-3d9a6.appspot.com/images/f5bd8360.jpeg")

// Create local filesystem URL
let localURL: URL! = URL(string: "file:///Documents/images/f5bd8360.jpeg")

// Download to the local filesystem
let downloadTask = imageRef.write(toFile: localURL) { (URL, error) -> Void in
if (error != nil) {
print("Uh-oh, an error occurred!")
print(error)
} else {
print("Local file URL is returned")
}
}
}

我找到了 another question我遇到了同样的错误,但从未得到完整的回答。我认为这个提议是对的。我没有写入文件的权限。但是,我不知道如何获得权限。有什么想法吗?

最佳答案

问题是在你写这行的那一刻:

let downloadTask = imageRef.write(toFile: localURL) { (URL, error) -> Void in
etc.

您还没有写入该 (localURL) 位置的权限。要获得权限,您需要在尝试向 localURL

写入任何内容之前编写以下代码
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let localURL = documentsURL.appendingPathComponent("filename")

通过执行此操作,您会将文件写入设备上的以下路径(如果您在真实设备上进行测试):file:///var/mobile/Containers/Data/Application/XXXXXXXetc.etc./Documents/filename

如果你在模拟器上测试,路径显然会在计算机上的某个地方。

关于ios - Firebase 存储下载到本地文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960210/

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