【问题标题】:Save image file to temp directory将图像文件保存到临时目录
【发布时间】:2016-03-03 01:15:34
【问题描述】:

我有一个名为“Image.png”的图像文件,它保存在我的主包中(就在项目导航器层次结构中的 ViewController.swift 文件旁边)。我想将此图像的副本保存到临时目录。我以前没做过,请问我可以使用什么代码?

【问题讨论】:

    标签: ios swift image temporary-directory


    【解决方案1】:

    这样的事情应该可以解决问题。我假设你想要 Swift 中的答案。

     /**
     * Copy a resource from the bundle to the temp directory.
     * Returns either NSURL of location in temp directory, or nil upon failure.
     *
     * Example: copyBundleResourceToTemporaryDirectory("kittens", "jpg")
     */
    public func copyBundleResourceToTemporaryDirectory(resourceName: String, fileExtension: String) -> NSURL?
    {
        // Get the file path in the bundle
        if let bundleURL = NSBundle.mainBundle().URLForResource(resourceName, withExtension: fileExtension) {
    
            let tempDirectoryURL = NSURL.fileURLWithPath(NSTemporaryDirectory(), isDirectory: true)
    
            // Create a destination URL.
            let targetURL = tempDirectoryURL.URLByAppendingPathComponent("\(resourceName).\(fileExtension)")
    
            // Copy the file.
            do {
                try NSFileManager.defaultManager().copyItemAtURL(bundleURL, toURL: targetURL)
                return targetURL
            } catch let error {
                NSLog("Unable to copy file: \(error)")
            }
        }
    
        return nil
    }
    

    虽然,我不太确定您为什么要这样做而不是直接访问捆绑资源。

    【讨论】:

    • 非常感谢,马特!
    • 没问题@dimery2006,如果有帮助请考虑接受答案:)
    【解决方案2】:

    Swift 4.x 版本的 mszaro 的回答

    /**
     * Copy a resource from the bundle to the temp directory.
    
     * Returns either NSURL of location in temp directory, or nil upon failure.
     *
     * Example: copyBundleResourceToTemporaryDirectory("kittens", "jpg")
     */
    public func copyBundleResourceToTemporaryDirectory(resourceName: String, fileExtension: String) -> NSURL?
    {
        // Get the file path in the bundle
        if let bundleURL = Bundle.main.url(forResource: resourceName, withExtension: fileExtension) {
    
            let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true)
    
            // Create a destination URL.
            let targetURL = tempDirectoryURL.appendingPathComponent("\(resourceName).\(fileExtension)")
    
            // Copy the file.
            do {
                try FileManager.default.copyItem(at: bundleURL, to: targetURL)
                return targetURL as NSURL
            } catch let error {
                NSLog("Unable to copy file: \(error)")
            }
        }
    
        return nil
    }
    

    【讨论】:

    • NSURL 不适用于 Swift 4。不要使用它。附加文件名和扩展名的正确语法是let targetURL = tempDirectoryURL.appendingPathComponent(resourceName).appendingPathExtension(fileExtension))
    【解决方案3】:

    这是 Swift 5

    中的答案
    /**
     * Copy a resource from the bundle to the temp directory.
    
     * Returns either URL of location in temp directory, or nil upon failure.
     *
     * Example: copyBundleResourceToTemporaryDirectory("kittens", "jpg")
     */
    
    public func copyBundleResourceToTemporaryDirectory(resourceName: String, fileExtension: String) -> URL?
        {
            // Get the file path in the bundle
            if let bundleURL = Bundle.main.url(forResource: resourceName, withExtension: fileExtension) {
    
                let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
    
                // Create a destination URL.
                let targetURL = tempDirectoryURL.appendingPathComponent(resourceName).appendingPathExtension(fileExtension)
    
                // Copy the file.
                do {
                    try FileManager.default.copyItem(at: bundleURL, to: targetURL)
                    return targetURL
                } catch let error {
                    print("Unable to copy file: \(error)")
                }
            }
    
            return nil
        }
    

    我建议阅读this article to learn more about temporary files

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 2022-01-16
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多