将图像文件保存到临时目录

时间:2016-03-03 01:15:35

标签: ios swift image temporary-directory

我有一个名为" Image.png"的图像文件,它保存在我的主包中(在Project Navigator层次结构中的ViewController.swift文件旁边)。我想将此映像的副本保存到临时目录。我之前从未做过,我可以使用哪些代码?

3 个答案:

答案 0 :(得分:16)

这样的事情应该可以解决问题。我假设你想要在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
}

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

答案 1 :(得分:3)

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
}

答案 2 :(得分:2)

这是 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

相关问题