首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在swift中按升序保存图片?

如何在swift中按升序保存图片?
EN

Stack Overflow用户
提问于 2021-08-12 18:52:50
回答 2查看 55关注 0票数 1

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

代码语言:javascript
复制
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....我该怎么做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-12 19:16:17

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

在UserDefaults中存储此索引的一种方法如下:

代码语言:javascript
复制
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中的某个位置,看看它是否正常工作。

这是你更新的方法...

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2021-08-12 19:00:56

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

代码语言:javascript
复制
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)
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68762802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档