error loop for UIImageJPEGRepresentation Swift

445 Views Asked by At

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:

error loop

3

There are 3 best solutions below

1
Vincenzo On BEST ANSWER

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

2
Davydov Denis On

if you using swift 4.2+ try to using only

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

otherwise, if you using swift 4.0, try to using only

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

don't forget recompile the code.

2
benc On

If you are in a Swift 4.0 project, and are using the newer 4.2 syntax, letting Xcode fix the code will lead to more errors, because the fix-logic doesn't seem to work well when you go backwards:

Example

mContact.imageData = image?.jpegData(compressionQuality: 1.0)

'jpegData(compressionQuality:)' has been renamed to 'UIImageJPEGRepresentation(::)' Replace 'jpegData' with 'UIImageJPEGRepresentation' [FIX]

(press FIX button)

mContact.imageData = image?.UIImageJPEGRepresentation(compressionQuality: 1.0)
                     ^^^^^^^ <- "Value of type 'UIImage' has no member 'UIImageJPEGRepresentation'

So you need to fix-the-fix.

(I can't find any explicit documentation on the web, but I assume that UIImageJPEGRepresentation used to be a class, rather than a method of the UIImage.)