I have folder with many files in documents directory. In my folder I have files with different extensions (jpg, png, mp3, zip). In my code I have stringArray
. I want to load all files in someArray
and after that add files with .png
extension from someArray
in stringArray
How to do it?
Is it possible to do this? Or I should find another way to load multiple files from documents directory?
I find answer for Load multiple images from the folder or directory. - Swift 4
I tried to use this code from answer:
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
}
But I can't get files from theItems
. Because theItems
it a string array. What I do wrong? How to get files from loadImagesFromAlbum
?
I tried to use this code:
images = loadImagesFromAlbum(folderName: "/folder1")
But it is not help me. I get only names. But I need to get files.
Update
I use this code to load image from documents directory
:
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("/folder1/1.png")
myImageView.image = UIImage(contentsOfFile: imageURL.path)
}
But I need to load 100 images from /folder1
in var images = [String]()
. Because I use var images = [String]()
to show images: contentVC.imageName = images[index] // and etc...
And the code I use above is not very convenient if I need add 40-100 images.
Update 1
I have this code to load images from my project and show it:
class PageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
for page in 1...pageNumber[indexPage] {
images.append("page\(page).png")
}
}
func getContentViewController(withIndex index: Int) -> ContentViewController? {
if index < images.count{
let contentVC = self.storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
contentVC.itemIndex = index
contentVC.imageName = images[index]
return contentVC
}
return nil
}
}
import UIKit
class ContentViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var itemIndex: Int = 0
var imageName: String?
override func viewDidLoad() {
super.viewDidLoad()
if let currentImage = imageName{
imageView.image = UIImage(named: currentImage)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
But I need to load images from documents directory and show it. How to do it?