NSFileManager编写和删除图像会导致内存崩溃

时间:2017-02-19 14:49:25

标签: ios swift crash nsfilemanager

我使用以下功能存储和删除图像。当我连续多次这样做时(存储20张图片,删除20张图片,存储20张图片......)我就崩溃了。

private let fileDirectory : URL  = {
    return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}()


func saveImageToDocuments(image: UIImage, imageName: String, errorCompletion: @escaping (FileSystemInteractorFailureResult) -> ()) {
        let imagePath = fileDirectory.appendingPathComponent("\(imageName).jpg")

        guard let imageData = UIImageJPEGRepresentation(image, 1) else {
            // handle failed conversion
            errorCompletion("camera__error_conversion".localized)
            return
        }
        do {
            try imageData.write(to: imagePath)
        } catch let error as NSError {
            errorCompletion(error.localizedDescription)
        }
    }

func removeImageFromDocuments(imageName: String, errorCompletion: @escaping (FileSystemInteractorFailureResult) -> ()) {
        let imagePath = fileDirectory.appendingPathComponent("\(imageName).jpg")
        do {
            try FileManager.default.removeItem(at: imagePath)
        } catch let error {
            errorCompletion(error.localizedDescription)
        }
    }

在iphone4s或iphone5上重现崩溃更容易。

[893:158934] [MC] Invalidating cache
[893:158475] [MC] Reading from public effective user settings.
[893:158475] [Common] _BSMachError: port 6c9b; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
[893:158475] [Common] _BSMachError: port 6c9b; (os/kern) invalid name (0xf) "Unable to deallocate send right"

看起来我正在使用标准方法。

崩溃日志:

  "crashReporterKey" : "f1a2bf545e9d887870712d8d96e6529df902e899",
  "kernel" : "Darwin Kernel Version 16.3.0: Thu Dec 15 22:41:46 PST 2016; root:xnu-3789.42.2~1\/RELEASE_ARM64_S5L8960X",
  "product" : "iPhone6,2",
  "incident" : "4BFAE23D-1EFC-43B3-93B5-E282655A8BE3",
  "date" : "2017-02-19 16:07:50.69 +0100",
  "build" : "iPhone OS 10.2.1 (14D27)",
  "timeDelta" : 3,
  "memoryStatus" : {
  "compressorSize" : 37521,
  "pageSize" : 4096,
  "compressions" : 11849970,
  "memoryPages" : {
    "active" : 34557,
    "throttled" : 0,
    "fileBacked" : 11956,
    "wired" : 155710,
    "anonymous" : 41857,
    "purgeable" : 16,
    "inactive" : 19210,
    "free" : 828,

0 个答案:

没有答案