菜鸟教程小白 发表于 2022-12-11 20:30:49

ios - 为什么 URL 在存储然后从本地文件系统检索时会被截断?


                                            <p><p>我已经生成了一个具有以下功能的 URL。我想将此保存到本地文件夹以供以后使用。但是,当我将其保存到本地文件夹,然后检索它时,URL 被截断。请有人建议我如何保存和提取完整的 URL?</p>
<pre><code>func createPhotoURL() -&gt; URL {
   
    let fileName = &#34;tempImage_wb.jpg&#34;
   
    let documentsDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentDirectory = documentsDirectories.first!
    let pdfPageURL = documentDirectory.appendingPathComponent(&#34;\(fileName)&#34;)
   
    return pdfPageURL
}
</code></pre>
<p>当我调用该函数时,我会得到完整的 URL:</p>
<pre><code>let imageURL = createPhotoURL() // CORRECT URL FOR FILE UPLAOD
print(&#34;imageURL: \(imageURL)&#34;)
</code></pre>
<p>控制台:</p>
<pre><code>file:///var/mobile/Containers/Data/Application/CDDE2FED-5AAB-4960-9ACF-33E7B42D05AE/Documents/tempImage_wb.jpg
</code></pre>
<p>将上面的 URL 保存到本地文件夹并检索它:</p>
<pre><code>UserDefaults.standard.set(imageURL, forKey: &#34;URL_IMAGE&#34;)
guard let retrievedURL = UserDefaults.standard.string(forKey: &#34;URL_IMAGE&#34;) else{return}
print(&#34;retrievedURL: \(retrievedURL)&#34;)
</code></pre>
<p>控制台:</p>
<pre><code>retrievedURL: ~/Documents/tempImage_wb.jpg
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>要回答您的问题,请使用 UserDefaults.standard.<strong>url</strong> 而不是 UserDefaults.standard.string</p>
<pre><code>guard let retrievedURL = UserDefaults.standard.url(forKey: &#34;URL_IMAGE&#34;) else{return}
</code></pre>
<p>但这只有在您保存路径并在同一个 session 中检索它时才有效。如果您使用上述代码并多次运行该应用程序,您将获得未截断的 url(如您所愿)。但是如果你检查得当,你会发现你实际上在不同的 session 中得到了不同的 url。</p>
<p>因此您不应该将其保存为 URL,而应将其保存为字符串。为此,您可以使用 <a href="https://developer.apple.com/documentation/foundation/url/1779984-absolutestring" rel="noreferrer noopener nofollow">absoluteString</a> URL 的属性</p>
<pre><code>let imageURL = createPhotoURL()
print(&#34;imageURL: \(imageURL)&#34;)
UserDefaults.standard.set(imageURL.absoluteString, forKey: &#34;URL_IMAGE&#34;)
</code></pre>
<p>要检索,您将首先从 UserDefaults 中检索保存的字符串,然后将其转换为 URL</p>
<pre><code>if let urlString = UserDefaults.standard.string(forKey: &#34;URL_IMAGE&#34;),
    let retrievedURL = URL(string: urlString) {

    print(&#34;retrievedURL: \(retrievedURL)&#34;)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么 URL 在存储然后从本地文件系统检索时会被截断?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52131524/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52131524/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么 URL 在存储然后从本地文件系统检索时会被截断?