0

我正在将收到的推送通知中的图像保存到磁盘。起初我尝试使用我通常在整个应用程序中使用的静态函数,但我无法引用它来自NotificationService.swift哪个通知扩展。所以我复制了文件中的函数来使用它,但是 Xcode 陷入了错误循环。无论我声明data它都会引发错误。如果从错误中执行更正,'jpegData(compressionQuality:)' has been renamed to 'UIImageJPEGRepresentation(_:_:)'它会抛出错误'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'你能看到这里发生了什么吗?这是功能:

func saveImage(imageName: String, image: UIImage) {


        guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

        let fileName = imageName
        let fileURL = documentsDirectory.appendingPathComponent(fileName)
        guard let data = UIImageJPEGRepresentation(image, 1)  else { return }
        guard let data2 = image.jpegData(compressionQuality: 0.75) else {return}
        guard let data3 = image.UIImageJPEGRepresentation(compressionQuality: 1) else {return}
//
        //Checks if file exists, removes it if so.
        if FileManager.default.fileExists(atPath: fileURL.path) {
            do {
                try FileManager.default.removeItem(atPath: fileURL.path)
                print("Removed old image")
            } catch let removeError {
                print("couldn't remove file at path", removeError)
            }

        }

        do {
            try data.write(to: fileURL)
        } catch let error {
            print("error saving file with error", error)
        }

    }

另外,为什么我不能将原始静态函数引用为Functions.saveImage?非常感谢一如既往。

错误循环:

错误循环

4

2 回答 2

1

发现了问题。我有使用 Swift 4.2 和应用程序 Swift 4.0 的 NotifiationService。我昨天检查时一定不小心设置了它。再次感谢大家真的很愚蠢的问题,但是嘿..我们现在知道这种循环错误点在 modules 中设置的不同 Swift 版本中。在这里学到的教训..干杯

于 2019-06-15T12:42:29.003 回答
0

如果您使用 swift 4.2+,请尝试仅使用

guard let data = image.jpegData(compressionQuality: 0.75) else {
    return
}

否则,如果您使用 swift 4.0,请尝试仅使用

guard let imageData = UIImageJPEGRepresentation(image, 0.8) else {
    return
}

不要忘记重新编译代码。

于 2019-06-15T12:03:37.133 回答