我使用此功能保存了图像:
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))
创建的名称
我该如何特别指定创建的文件名
当我以图像名称为例时,“测试” 图像在单元格中通过
但是在我的应用程序用户中选择图像,但我不知道用户选择了哪个图像