gpt4 book ai didi

ios - 在 Swift 中从 Realm 加载图像路径的问题

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

在我的应用程序中,我将图像存储在本地存储中,并将该图像的路径保存在我的 Realm 数据库中。现在我无法从该路径加载此图像?

这就是我保存数据库路径的方式:

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory,nsUserDomainMask, true)
let dirPath = paths.first


let imageURL1 = URL(fileURLWithPath: dirPath!).appendingPathComponent("refuge1.jpg")

let med1 = Meditation()
med1.name = "Refuge"
med1.count = 0
med1.targetAmount = 111111
med1.malasStep = 108
med1.imagePath = imageURL1.path
med1.id = 1

尝试从此 meditation.imagePath 路径获取图像很简单。我仔细检查了路径,图像是否仍然无法使用此路径设置图像,是否缺少某些内容?

在 Debug模式下我看到了这个:

Meditation {
name = Refuge;
count = 0;
targetAmount = 111111;
malasStep = 108;
imagePath = /Users/macbook/Library/Developer/CoreSimulator/Devices/2E25309F-D6A9-41C3-9EF4-67203142172C/data/Containers/Data/Application/F198640B-3C72-4F9C-8173-FB00D3ABEC15/Documents/refuge1.jpg;
id = 1;}

但我的变量图像在 Debug模式下仍然为零

// Configure the cell...
cell.nameOfMeditation.text = meditation.name
cell.countOfMeditation.text = String(meditation.count)
let image = UIImage(contentsOfFile: meditation.imagePath)
cell.imageForMeditation.image = image

return cell

我看到了冥想和声音的名称,没有我的天啊。

最佳答案

不建议在 iOS 应用程序中保存文件的绝对文件路径(即所有内容,包括 /Users/macbook/Library/Developer/...),在 Realm 或其他任何地方.

出于安全原因,iOS 设备会在两次启动之间重命名 UUID 文件夹名称。这意味着虽然文件夹路径在保存时有效,但以后就不会了。

相反,建议只保存文件的相对路径(例如,它相对于 Documents 文件夹的位置。在这种情况下,它只是 /refuge1。 jpg),然后根据需要通过请求 Documents 目录路径动态构建绝对路径。

关于ios - 在 Swift 中从 Realm 加载图像路径的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539380/

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