gpt4 book ai didi

swift - 来自 NSItemProvider 的 URL 的有效时间

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

我正在编写一个共享扩展程序,它将接受图像并对其执行一些操作。在我的 UIViewController 子类的方法中,我可以通过编写以下代码访问文件的特定表示形式的 URL:

guard let context = self.extensionContext else {
return
}
guard let items = context.inputItems as? [NSExtensionItem] else {
return
}

for item in items {
guard let attachments = item.attachments else {
continue
}

for attachment in attachments {
guard attachment.hasItemConformingToTypeIdentifier("public.jpeg") else {
continue
}
attachment.loadFileRepresentation(forTypeIdentifier: "public.jpeg") { (url, error) in
if let url = url {
// How long is this "url" valid?
}
}
}
}

在我传递给 loadFileRepresentation(forTypeIdentifier:completionHandler:) 的 block 中,我得到了一个文件的 URL——在本例中是一个 JPEG。我可以假设这个 URL 的有效期有多长吗?具体来说,将 URL 本身写入某个共享存储区域以便我的应用程序稍后可以访问指向的文件是否安全?或者我是否应该假设 URL 是短暂的,如果我想访问它指向的文件,我应该在这个 block 中制作我自己的文件副本?

最佳答案

loadFileRepresentation 的文档指出:

This method writes a copy of the file’s data to a temporary file, which the system deletes when the completion handler returns.

所以 url 对完成处理程序的右花括号有效。

如果您需要在完成处理程序之外访问文件,则需要在完成处理程序完成之前将文件复制到沙箱的已知位置。

关于swift - 来自 NSItemProvider 的 URL 的有效时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56031648/

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