gpt4 book ai didi

iOS/Swift 无法将文件写入为将文件路径视为目录路径

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

<分区>

我有以下 swift 函数,我希望它能将传入的字节保存到 iOS 上的 JPEG 文件中。不幸的是,调用 data.write 引发了异常,我收到了错误消息

The folder “studioframe0.jpg” doesn’t exist. writing to file:/var/mobile/Containers/Data/Application/2A504F84-E8B7-42F8-B8C3-3D0A53C1E11A/Documents/studioframe0.jpg -- file:///

为什么 iOS 认为它是一个不存在的目录的目录路径,而不是我要求它写入的文件?

func saveToFile(data: Data){
if savedImageCount < 10 {
guard let documentDirectoryPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
let imgPath = URL(fileURLWithPath: documentDirectoryPath.appendingPathComponent("studioframe\(savedImageCount).jpg").absoluteString)
savedImageCount += 1
do {
try data.write(to: imgPath, options: .atomic)
print("Saved \(imgPath) to disk")
} catch let error {
print("\(error.localizedDescription) writing to \(imgPath)")
}
}
}

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