如何将多个图像文件迭代写入 swift swiftui ios 中的文档文件夹

How to write multiple image files iteratively to documents folder in swift swiftui ios

@ObservedObject foldersData = FoldersViewModel()    

func genThumbnails() -> Void {
        for ind in subDirNames.indices{
            for indd in galContents[ind].indices{
                if foldersData.subDirNames[ind] == "Root Folder"{
                    foldersData.genThumbnailFiles(FoldersViewModel.resizedImage(at: foldersData.galContents[ind][indd], for: CGSize(width: FoldersViewModel.imageSize[0]/4, height: FoldersViewModel.imageSize[1]/4))!, filename: (foldersData.galContents[ind][indd] as NSString).lastPathComponent, directoryPath: NSHomeDirectory().appending("/Documents/Thumbnails/"))}
                else{
                    foldersData.genThumbnailFiles(FoldersViewModel.resizedImage(at: foldersData.galContents[ind][indd], for: CGSize(width: FoldersViewModel.imageSize[0]/4, height: FoldersViewModel.imageSize[1]/4)) ?? UIImage(), filename: (foldersData.galContents[ind][indd] as NSString).lastPathComponent, directoryPath: NSHomeDirectory().appending("/Documents/Thumbnails/" + foldersData.subDirNames[ind]))
                }
                counts[0] = counts[0] + 1
            }
            counts[1] = counts[1] + 1
        }
        //isLoading.toggle()
        foldersData.isLoading = false
    }

func genThumbnailFiles(_ chosenImage: UIImage, filename: String, directoryPath: String) -> String {
    //let directoryPath =  NSHomeDirectory().appending("/Documents/Thumbnails/")
    if !FileManager.default.fileExists(atPath: directoryPath) {
        do {
            try FileManager.default.createDirectory(at: NSURL.fileURL(withPath: directoryPath), withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error)
        }
    }

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyyMMddhhmmss"

    //let filename = dateFormatter.string(from: Date()).appending(".jpg")
    //let filepath = directoryPath.appending(filename)
    let filepath = directoryPath+"/\(filename)"
    let url = NSURL.fileURL(withPath: filepath)
    print(directoryPath.appending(filename))
    do {
        try chosenImage.jpegData(compressionQuality: 1.0)?.write(to: url, options: .atomic)
        return String.init(directoryPath+"/\(filename)")
    } catch {
        print(error)
        print("file cant not be save at path \(filepath), with error : \(error)");
        return filepath
    }
}

所以我正在尝试为 ios 开发一个应用程序,它可以让我从我的旧设备上查看我的旧照片(我曾经拥有多种类型的设备,例如 android、windows phone 等),

所以,为了处理缩略图,我想在开始时通过调整它们的大小并将它们添加到应用程序文档目录来处理它们,这样我就不需要每次加载缩略图时都调整大小,

但是当我尝试这样做时,在任意文件写入过程中弹出错误,说无法分配内存:

/var/mobile/Containers/Data/Application/A5FD35C8-B41D-43BC-A809-4C2BEA92DDEA/Documents/US - Pixel 2 2019-08-31/IMG_20181003_145023.jpg 2021-08-27 03:54:43.687807-0400 myapp[5612:701316] [myapp] _CFDataCreateWithMappedFile:181: could not mmap '/var/mobile/Containers/Data/Application/A5FD35C8-B41D-43BC-A809-4C2BEA92DDEA/Documents/US - Pixel 2 2019-08-31/IMG_20181003_145023.jpg': error: 12 (Cannot allocate memory) myapp(5612,0x10059b880) malloc: can't allocate region

我还看到在我的 jpg 写入过程中,内存 (RAM) 使用量逐渐增加(比如从 2​​5mb 到 250mb),

任何东西都会有帮助,我尝试使用第三方库(例如 Nuke)在滚动过程中加载缩略图,但是,该过程非常耗电,并且旨在用于从我假设的在线服务器加载较小数量的文件,

我有数千张照片,希望能够像在照片应用程序中一样浏览,不幸的是,苹果很难将外部文件加载到自己的照片应用程序并进行整理。

iOS中的对象保证只有在离开autoreleasepool的范围后才会被释放。每次更新帧时都会运行一个默认池。

如果你在一个周期内多次分配和释放内存,你可以添加自己的autoreleasepool来保证这块内存立即释放:

func genThumbnails() -> Void {
    for ind in subDirNames.indices{
        for indd in galContents[ind].indices{
            autoreleasepool {
                if foldersData.subDirNames[ind] == "Root Folder"{
                    foldersData.genThumbnailFiles(FoldersViewModel.resizedImage(at: foldersData.galContents[ind][indd], for: CGSize(width: FoldersViewModel.imageSize[0]/4, height: FoldersViewModel.imageSize[1]/4))!, filename: (foldersData.galContents[ind][indd] as NSString).lastPathComponent, directoryPath: NSHomeDirectory().appending("/Documents/Thumbnails/"))}
                else{
                    foldersData.genThumbnailFiles(FoldersViewModel.resizedImage(at: foldersData.galContents[ind][indd], for: CGSize(width: FoldersViewModel.imageSize[0]/4, height: FoldersViewModel.imageSize[1]/4)) ?? UIImage(), filename: (foldersData.galContents[ind][indd] as NSString).lastPathComponent, directoryPath: NSHomeDirectory().appending("/Documents/Thumbnails/" + foldersData.subDirNames[ind]))
                }
            }
            counts[0] = counts[0] + 1
        }
        counts[1] = counts[1] + 1
    }
    //isLoading.toggle()
    foldersData.isLoading = false
}