I'm saving an image to disk from received push notification.At first I tried to use the static function that I normally use throughout my app, but I couldn't reference it from NotificationService.swift
which is the notification extension. So I copied the function in the file to use it, but Xcode got stuck in a error loop. No matter ho I declare data
it would throw an error. If perform the correction from error 'jpegData(compressionQuality:)' has been renamed to 'UIImageJPEGRepresentation(_:_:)'
the it throws the error 'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'
Can you see what's happening here? This is the function :
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)
}
}
Also, why can't I reference to the original static function as Functions.saveImage
?
Many thanks as always.
Error loop:
Found the problem. I had the NotifiationService using Swift 4.2 and the app Swift 4.0. I must have accidentally set it yesterday when checking.. Thanks again guys really silly problem but hey.. we'll now know that this kind of loop error points in different Swift version set in modules . Lesson learned here.. Cheers