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

Ios 将图像文件保存到临时目录,ios,swift,image,temporary-directory,Ios,Swift,Image,Temporary Directory,我有一个名为“image.png”的图像文件,它保存在我的主包中(在项目导航器层次结构中的ViewController.swift文件旁边)。我想将此图像的副本保存到临时目录。我以前从未这样做过,请问我可以使用什么代码?像这样的东西应该可以解决这个问题。我猜你想用斯威夫特回答 /** * Copy a resource from the bundle to the temp directory. * Returns either NSURL of location in temp dire

我有一个名为“image.png”的图像文件,它保存在我的主包中(在项目导航器层次结构中的ViewController.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
}

尽管如此,我不确定您为什么要这样做,而不是直接访问bundle资源。

Swift 4.xmszaro答案的版本

/**
 * 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
}

以下是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
    }

我建议你读一读

谢谢你,马特!“没问题”@ DimeRe2006,如果有帮助请考虑接受答案:<代码> NSURL<代码>不适合SWIFT 4。不要用那个。附加文件名和扩展名的正确语法是
let targetURL=tempDirectoryURL.appendingPathComponent(resourceName).appendingPathExtension(fileExtension))