从文件目录swift获取图像

时间:2022-08-11 00:32:17

Say I were using this code to save an image to the documents directroy

假设我正在使用此代码将图像保存到documents directroy

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
    if let dirPath = paths[0] as? String {
        let readPath = dirPath.stringByAppendingPathComponent("Image.png")
        let image = UIImage(named: readPath)
        let writePath = dirPath.stringByAppendingPathComponent("Image2.png") 
        UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true)
    }
  }
}

How would I then retrive it? Keeping in mind than in iOS8 the exact path changes often

那么我该如何找回它呢?记住,在iOS8中,确切的路径经常发生变化

4 个解决方案

#1


65  

You are finding the document directory path at runtime for writing the image, for reading it back, you can use the exact logic:

您正在运行时查找文档目录路径,用于编写映像,用于读取映像,您可以使用精确的逻辑:

Swift 3

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath          = paths.first
{
   let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png")
   let image    = UIImage(contentsOfFile: imageURL.path)
   // Do whatever you want with the image
}

Swift 2

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask    = NSSearchPathDomainMask.UserDomainMask
if let paths            = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
{
     if paths.count > 0
     {
         if let dirPath   = paths[0] as? String
         {
             let readPath = dirPath.stringByAppendingPathComponent("Image2.png")
             let image    = UIImage(contentsOfFile: readPath)
             // Do whatever you want with the image
         }
     }
}

#2


6  

Swift 2

斯威夫特2

If you want to get a file from your document directory in Swift 2:

如果您想要从您的文档目录在Swift 2:

let path: String? = NSBundle.mainBundle().pathForResource("imageName", ofType: "png", inDirectory: "DirectoryName/Images")
let imageFromPath = UIImage(contentsOfFile: path!)!
self.myImage.image = imageFromPath

Hope that helps somebody

希望能帮助别人

#3


6  

Better as an extension.

更好的为一个扩展。

extension URL {
    static var documentsDirectory: URL {
        let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        return try! documentsDirectory.asURL()
    }

    static func urlInDocumentsDirectory(with filename: String) -> URL {
        return documentsDirectory.appendingPathComponent(filename)
    }
}

Used like this:

使用这样的:

let path = URL.urlInDocumentsDirectory(with: filename).path
let image = UIImage(contentsOfFile: path)

#4


2  

Load multiple images from the folder or directory. - Swift 4

从文件夹或目录加载多个映像。-斯威夫特4

Here's the image attached to show, what we want to achieve in the given below code. 从文件目录swift获取图像

这是所附的图像,显示了我们希望在下面的代码中实现的内容。

Here's the code to find the multiple images from the folder in documents directory. I have written one method to do the same.

下面是从documents目录中的文件夹中查找多个映像的代码。我写了一个方法来做同样的事情。

In the code we are passing the "Folder Name" (ie. Red) and getting the contents of that directory. In return we got the array of images name.

在代码中,我们传递的是“文件夹名”。红色)并获取该目录的内容。作为回报,我们得到了图像数组的名称。

    static func loadImagesFromAlbum(folderName:String) -> [String]{

    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
    let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
    var theItems = [String]()
    if let dirPath          = paths.first
    {
        let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(folderName)

        do {
            theItems = try FileManager.default.contentsOfDirectory(atPath: imageURL.path)
            return theItems
        } catch let error as NSError {
            print(error.localizedDescription)
            return theItems
        }
    }
    return theItems
}

Here's the result of given code. 从文件目录swift获取图像

这是给定代码的结果。

Hope it helps.

希望它可以帮助。

Thanks

谢谢

#1


65  

You are finding the document directory path at runtime for writing the image, for reading it back, you can use the exact logic:

您正在运行时查找文档目录路径,用于编写映像,用于读取映像,您可以使用精确的逻辑:

Swift 3

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath          = paths.first
{
   let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png")
   let image    = UIImage(contentsOfFile: imageURL.path)
   // Do whatever you want with the image
}

Swift 2

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask    = NSSearchPathDomainMask.UserDomainMask
if let paths            = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
{
     if paths.count > 0
     {
         if let dirPath   = paths[0] as? String
         {
             let readPath = dirPath.stringByAppendingPathComponent("Image2.png")
             let image    = UIImage(contentsOfFile: readPath)
             // Do whatever you want with the image
         }
     }
}

#2


6  

Swift 2

斯威夫特2

If you want to get a file from your document directory in Swift 2:

如果您想要从您的文档目录在Swift 2:

let path: String? = NSBundle.mainBundle().pathForResource("imageName", ofType: "png", inDirectory: "DirectoryName/Images")
let imageFromPath = UIImage(contentsOfFile: path!)!
self.myImage.image = imageFromPath

Hope that helps somebody

希望能帮助别人

#3


6  

Better as an extension.

更好的为一个扩展。

extension URL {
    static var documentsDirectory: URL {
        let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        return try! documentsDirectory.asURL()
    }

    static func urlInDocumentsDirectory(with filename: String) -> URL {
        return documentsDirectory.appendingPathComponent(filename)
    }
}

Used like this:

使用这样的:

let path = URL.urlInDocumentsDirectory(with: filename).path
let image = UIImage(contentsOfFile: path)

#4


2  

Load multiple images from the folder or directory. - Swift 4

从文件夹或目录加载多个映像。-斯威夫特4

Here's the image attached to show, what we want to achieve in the given below code. 从文件目录swift获取图像

这是所附的图像,显示了我们希望在下面的代码中实现的内容。

Here's the code to find the multiple images from the folder in documents directory. I have written one method to do the same.

下面是从documents目录中的文件夹中查找多个映像的代码。我写了一个方法来做同样的事情。

In the code we are passing the "Folder Name" (ie. Red) and getting the contents of that directory. In return we got the array of images name.

在代码中,我们传递的是“文件夹名”。红色)并获取该目录的内容。作为回报,我们得到了图像数组的名称。

    static func loadImagesFromAlbum(folderName:String) -> [String]{

    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
    let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
    var theItems = [String]()
    if let dirPath          = paths.first
    {
        let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(folderName)

        do {
            theItems = try FileManager.default.contentsOfDirectory(atPath: imageURL.path)
            return theItems
        } catch let error as NSError {
            print(error.localizedDescription)
            return theItems
        }
    }
    return theItems
}

Here's the result of given code. 从文件目录swift获取图像

这是给定代码的结果。

Hope it helps.

希望它可以帮助。

Thanks

谢谢