我正在将收到的推送通知中的图像保存到磁盘。起初我尝试使用我通常在整个应用程序中使用的静态函数,但我无法引用它来自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
?非常感谢一如既往。
错误循环: