0

I saved image with this func:

let image = categoryImage.image
        let data = image!.jpegData(compressionQuality: 0.5)!
        let name = "\(Int(Date().timeIntervalSince1970))"
        do {
            let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
            let fileURL = documentDirectory.appendingPathComponent(name)

            try data.write(to: fileURL)

            print(fileURL)

        } catch {
            print(error.localizedDescription)
        }

I get image from this:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellPrototype

        // Configure the cell...
        let currentItem = item[indexPath.row]
        cell.pointNameLabel.text = currentItem

        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("test")
            let image    = UIImage(contentsOfFile: imageURL.path)
            cell.categoryImage.image = image
        }
        return cell
    }

When i get image back i can't take it's name which was created by (Int(Date().timeIntervalSince1970))

How I can take specifically the file name that was created

When i take image name for example "test" image pass in cell

But in my app user select image and i don't know which image user selected

Bogdan
  • 31
  • 3
  • Well, maybe you should save the filename somewhere? If there is more than one image in that directory you should save the filename or come up with some logic that would help you understand which image should be retrieved next. For example, if your table view displays images in the order they are created, you can get all the images from the directory, sort them by their filenames and display in that order. – pckill Mar 05 '19 at 16:27
  • @pckill thank you for answering, but i don't understand how to do that. I think about this but I lack experience in swift. Can you make an example for me? – Bogdan Mar 05 '19 at 16:43
  • If you are sure the number of items it is equal of the number of images in your document directory all you need is to get the contents of the document directory and fill an array with the images and sort it by its name. (should be the same as sorting by date). Btw you should provide a pathExtension `.jpeg` to the file name – Leo Dabus Mar 05 '19 at 19:01
  • @LeoDabus can you write an example? – Bogdan Mar 06 '19 at 07:36

0 Answers0