【问题标题】:How to save images in ascending order in swift?如何快速按升序保存图像?
【发布时间】:2021-10-16 03:24:58
【问题描述】:

我正在构建一个应用程序,在该应用程序中我使用 FileManager 使用设备相机保存一些图像。所以现在我将文件名保存为 Doc-Time。 我正在使用下面的代码,

func saveImageToDocumentDirectory(image: UIImage ) {
    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "hh:mm:ss"
    
    let fileName = "Doc-" + dateFormatter.string(from: Date())
    let fileURL = documentsDirectory.appendingPathComponent(fileName
    )
    if let data = image.jpegData(compressionQuality: 1.0),!FileManager.default.fileExists(atPath: fileURL.path){
        do {
            try data.write(to: fileURL)
            print("file saved")
        } catch {
            print("error saving file:", error)
        }
    }
}

但我想在这里,Doc-1,Doc-2,Doc-3.... 我该怎么做?

【问题讨论】:

    标签: swift nsfilemanager


    【解决方案1】:

    您可以通过简单地存储图像的下一个索引来实现这一点。首先,当您使用将图像命名为 Doc-1 时,索引应该为 1,然后索引中有 2,依此类推....

    一种将此索引存储在 UserDefaults 中的方法,例如:

    var nextImageIndex: Int {
        UserDefaults.standard.integer(forKey: "NextImageIndex") + 1  //+1 if you want to start with 1
    }
        
    func incrementImageIndex() {
        UserDefaults.standard.setValue(nextImageIndex, forKey: "NextImageIndex")
    }
    

    将上面的代码放在 UIViewController 的某个地方,看看它是否有效。

    这是您更新的方法...

    func saveImageToDocumentDirectory(image: UIImage ) {
        guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
            return
        }
        
        let fileName = "Doc-\(nextImageIndex)"
        let fileURL = documentsDirectory.appendingPathComponent(fileName)
        let fileAlreadyExists = FileManager.default.fileExists(atPath: fileURL.path)
        if let data = image.jpegData(compressionQuality: 1.0), !fileAlreadyExists {
            do {
                try data.write(to: fileURL)
                incrementImageIndex()
                print("file saved")
            } catch {
                print("error saving file:", error)
            }
        }
    }
    

    【讨论】:

    • 您好,感谢您的回复。我看不懂,你能用我的函数解释一下吗?
    • 好的,让我用你的函数更新我的答案。
    • 您好,感谢您的回答,它工作正常,但是当我从 1 开始时,它保存为 Doc-1、Doc-3、Doc-5、Doc-7
    • 不,它会将图像保存为 Doc-1、Doc-2、Doc-3 等等
    • 尝试打印文件名
    【解决方案2】:

    创建一个变量来存储文档计数并在每次保存到文档目录时递增它,然后在字符串中使用该值。

    let documentKey = "documentIndex" 
    
    @objc var documentIndex: Int {
        get { UserDefaults.value(forKey: documentKey) as? Int ?? 0 }
        set { UserDefaults.setValue(newValue, forKey: documentKey) }
    }
    
    func saveImageToDocumentDirectory(image: UIImage ) {
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        
        documentIndex += 1
        
        let fileName = "Doc-\(documentIndex)"
        let fileURL = documentsDirectory.appendingPathComponent(fileName
        )
        if let data = image.jpegData(compressionQuality: 1.0),!FileManager.default.fileExists(atPath: fileURL.path){
            do {
                try data.write(to: fileURL)
                print("file saved")
            } catch {
                print("error saving file:", error)
            }
        }
    }
    

    【讨论】:

    • 这仅适用于单个应用会话并在下一个会话中重复。
    • 嗨,clawesome,它工作正常,直到应用程序第一次运行,但是当应用程序重新启动时它不能正常工作,我的意思是当我第一次启动应用程序并保存 2 个文件时,它工作了,然后我重新启动了我的应用程序,在这种情况下,我必须点击三下才能获得 Doc-3
    • 您没有指定需要在会话之间保留值。我可以调整代码来解决这个问题,但将来您应该尝试更具体地满足您的所有要求。
    • @AbhishekKumar 我更新了代码,使值在会话之间保持不变。
    • 感谢clawesome,我会更具体地提出我的要求。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2019-05-22
    • 2019-06-19
    相关资源
    最近更新 更多