0

我用这个函数保存了图像:

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)
        }

我从中得到图像:

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
    }

当我取回图像时,我无法使用它创建的名称(Int(Date().timeIntervalSince1970))

我如何才能具体使用创建的文件名

当我取图像名称时,例如“测试”图像通过单元格

但是在我的应用程序中,用户选择图像,我不知道用户选择了哪个图像

4

0 回答 0