gpt4 book ai didi

swift - 文档目录 : delete file and rename remaining files in Swift 4. 2

转载 作者:行者123 更新时间:2023-11-30 10:54:04 24 4
gpt4 key购买 nike

这与此 SO 帖子中解决的问题相同:How to rename files in documents directory但这对我一点帮助都没有。这是我遇到的唯一一篇似乎与我的问题相关的帖子。

我有一个 Collection View ,其中图像是通过 didFinishPickingMediaWithInfo 中的 imagePickerController 添加的。它循环遍历文档目录,并根据下一个可用的索引号为所选图像命名。因此图像被命名为 image1.jpg、image2.jpg 等。当图像加载到 collectionView 中时,我再次循环并按索引顺序显示图像。当需要删除图像时,我可以在文档目录中找到它并将其删除,但索引顺序会被中断,我无法再加载图像。因此,如果我删除 image3.png,文档目录将包含 image1.jpg、image2.jpg、image4.jpg、image5.jpg 等。

我当前尝试重命名或移动目录中图像的代码仍在索引号上循环,并且当涉及到空索引(image3.jpg)时自然停止。如何使 image4.jpg 移动到 image3.jpg 原来所在的位置(并移动删除图像的索引后面的所有其他图像)?

我无法完全理解重命名文件所需的逻辑。如果您需要更多代码,请告诉我。感谢所有建议。这是我目前拥有的:

@objc func tapToDelete(recognizer: UITapGestureRecognizer)
{

let pointInCollectionView = recognizer.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: pointInCollectionView)

countIndex = (indexPath?.row)! + 1

let filepath = getFolderAndFilePath() //this gets the folder and filepath of each image


let alert = UIAlertController(title: "Delete This Image?", message: "Are you sure? This cannot be undone.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { (action) in
do
{
try self.fileManager.removeItem(at: filepath as URL)
self.imageArray.remove(at: self.countIndex)


self.folderPath = self.getFolderPath()
if self.folderPath != nil
{
var j = 0
var z = 0
var finalArray = [String]()

do
{
let directoryContent = try FileManager.default.contentsOfDirectory(at: self.folderPath! as URL, includingPropertiesForKeys: nil, options: [])


for _ in directoryContent
{
z += 1
let fileString = String(format: "image%d.jpg", z)
let oldPath = self.folderPath?.appendingPathComponent(fileString)
print("filename oldPath 😣: \(String(describing: oldPath))")

if self.fileManager.fileExists(atPath: (oldPath?.path)!)
{
j += 1
let newFileName = String(format: "image%d.jpg", j)


finalArray.append(newFileName)

let newPath = oldPath?.deletingLastPathComponent().appendingPathComponent(newFileName)
print("new filename: \(String(describing: newFileName))")

_ = try? self.fileManager.moveItem(atPath: (oldPath?.path)!, toPath: (newPath?.path)!)

}
else
{
print("no file here")
//this is called when we get to the image removed(image3.jpg)
}
self.collectionView.reloadData() //this reloads the collectionview and updates the count but still shows image3 instead of image4 even though image3 has been removed from the directory
}
}
catch
{
print("Could not search for urls of files in documents directory: \(error)")
}
}
}
catch
{
print(error)

}

}))
self.present(alert, animated: true)
}

最佳答案

这可能需要一些调整,但是删除文件后以下方法(伪代码)怎么样?

let directoryContent = try FileManager.default.contentsOfDirectory(at:   self.folderPath! as URL, includingPropertiesForKeys: nil, options: [])
var index = 1
var newImageArray:[URL] = []
for fileURL in directoryContent
{
let newFileName = String(format: "image%d.jpg", index)
let newFilePathURL = makePathAsYouWere()
_ = try? self.fileManager.moveItem(atPath: fileURL, toPath: newFilePathURL)
index += 1
newImageArray.append(newFilePathURL)
}

self.imageArray = newImageArray

collectionView.reload()

本质上,一旦删除文件,您将拥有一组可以迭代的文件。该列表可能需要排序才能使它们按正确的顺序排列,但一旦它们排序(文件 1、文件 2、文件 4、文件 5)。您应该能够按顺序重命名每个文件,以使它们按数字顺序恢复,没有间隙,因为上面的方法不关心旧文件的索引。

对于图像数组,您应该能够使用您正在创建的新文件路径创建一个新数组,替换旧数组,然后触发重新加载。这样,您也不必从图像数组中删除删除项,它会在您重建内容时删除。

请告诉我这是否足以让您开始,否则,我可以尝试整理一个完整的示例。

关于swift - 文档目录 : delete file and rename remaining files in Swift 4. 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com