快速将多个图像一次性保存到文件系统中

时间:2019-05-01 07:35:46

标签: swift recursion memory uiimage realm

我具有以下功能,我需要清除数据库中的所有图片列,然后移至文件系统。当我一次完成所有这些操作时,内存太多,它将崩溃。我切换到递归函数,并进行20次写入和批处理。

我大约需要做6张桌子。我的Realm数据库中有2个半月的数据。在我调用6个递归函数后,将其切换为40mb,取出图像并压缩Realm。

由于调用了我的函数,我看到的内存使用率很高,而RAM较少的手机将无法处理它。

如何在每个功能之间释放内存和CPU?

public static func clearEqCatPics(){

        let docsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let eqcatPicDir = docsDir.appendingPathComponent(util_Constants.DIR_EQCAT_PICS)

        do {
            var realm : Realm? = try Realm()
            let predicate = NSPredicate(format: "icon != %@", "")
            let categories = realm!.objects(STD_EQ_category.self).filter(predicate).sorted( by: [SortDescriptor(keyPath: "displayorder", ascending: true), SortDescriptor(keyPath: "_id", ascending: true)] )

            if (categories.count > 0)
            {
                realm?.beginWrite()
                let upper = categories.count > 20 ? 20 : categories.count

                var actualCounter = upper
                for i in 0..<upper{
                    autoreleasepool{
                        if let proPicData = Data(base64Encoded: categories[actualCounter - 1].icon, options: .ignoreUnknownCharacters) {


                            let filename = eqcatPicDir.appendingPathComponent(categories[actualCounter - 1]._id.description+".jpg")

                            (proPicData as NSData).writeToURL2(named: filename, completion: { (result, url) -> Void in

                            })

                            categories[actualCounter - 1].icon = ""

                        }
                        else{
                            categories[actualCounter - 1].icon = ""
                        }
                    }

                    actualCounter = actualCounter - 1

                }

                try realm?.commitWrite()
                let eqcatNew = realm!.objects(STD_EQ_category.self).filter(predicate)
                print("$$$$$$$$$$$$$$$$$$$$ 2. eqcatNew COUNT : \(eqcatNew.count) $$$$$$$$$$$$$$$$$$$$")
                realm = nil
                if eqcatNew.count > 0 {
                    clearEqCatPics()
                }
            }
            realm = nil
        }
        catch let error as NSError {
            print("error realm \(error.localizedDescription)")

        }

    }

writeToURL2在哪里:

我需要摆脱扩展中的弱者,因为我已经越过了守卫,让多个物品被跳过了

extension NSData {

    func writeToURL2(named:URL, completion: @escaping (_ result: Bool, _ url:NSURL?) -> Void)  {


        let tmpURL = named as NSURL

        //[weak self]
        DispatchQueue.global(qos: .background).async {  () -> Void in

            //guard let strongSelf = self else { print("self was weak"); completion (false, tmpURL); return }

            self.write(to: tmpURL as URL, atomically: true)
            var error:NSError?
            if tmpURL.checkResourceIsReachableAndReturnError(&error) {
                print("We have it")
                completion(true, tmpURL)
            } else {
                print("We Don't have it\(error?.localizedDescription)")
                completion (false, tmpURL)
            }

        }

    }
}

编辑

我将for循环中的writeToURL更改为以下内容:

do {
     try proPicData.write(to: filename, options: [.atomic])                               
}
catch let err as NSError{
     print("err : \(err.localizedDescription)")
}

它有助于记忆,但有时我会得到Thread1:EXC_BAD_ACCESS指向该行尝试proPicData.write ...

仍然具有很高的CPU使用率。无论如何,是否需要清除每个函数调用之间的CPU使用率?

1 个答案:

答案 0 :(得分:0)

您正在同时获取Realm中的所有对象,这就是占用大量内存的原因

let categories = realm!.objects(STD_EQ_category.self).filter(predicate).sorted( by: [SortDescriptor(keyPath: "displayorder", ascending: true), SortDescriptor(keyPath: "_id", ascending: true)] )

写入文件不是消耗内存,尽管这可能与CPU使用率有关。

我建议在提取的类别上放置limit,以免将所有类别及其图像加载到内存中。否则,尝试提出一个提取谓词,否则将以一种合理的方式限制数据。