ios 从文档目录快速获取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29005381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get image from documents directory swift
提问by Learnin
Say I were using this code to save an image to the 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 中的确切路径经常变化
回答by Midhun MP
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 and Swift 4.2
斯威夫特 3 和斯威夫特 4.2
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
斯威夫特 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
}
}
}
回答by Reid
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)
回答by Harjot Singh
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.
这是附加的图像,用于显示我们想要在下面给出的代码中实现的目标。
Here's the code to find the multiple images from the folder in documents directory. I have written one method to do the same.
这是从文档目录中的文件夹中查找多个图像的代码。我写了一种方法来做同样的事情。
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.
Hope it helps.
希望能帮助到你。
Thanks
谢谢
回答by smoosh911
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
希望能帮助某人