我正在将接收到的推送notification.At中的图像保存到磁盘中。首先,我尝试使用在整个应用程序中通常使用的静态函数,但无法从通知扩展NotificationService.swift中引用它。所以我复制了文件中的函数来使用它,但是Xcode陷入了一个错误循环。无论我如何声明data,它都会抛出一个错误。如果从error 'jpegData(compressionQuality:)' has been renamed to 'UIImageJPEGRepresentation(_:_:)'执行更正,它抛出了error '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?非常感谢,一如既往。
错误循环:

发布于 2019-06-15 20:42:29
找到了问题所在。我用的是Swift 4.2和应用Swift 4.0的NotifiationService。我一定是昨天检查的时候不小心把它设置好了。再次感谢,伙计们,真的是个愚蠢的问题,但是嘿..我们现在知道,这种循环错误点在模块中的不同Swift版本集中。从这里学到的教训..干杯
发布于 2019-06-15 20:03:37
如果您使用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
}别忘了重新编译代码。
https://stackoverflow.com/questions/56609563
复制相似问题