0

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

Vincenzo
  • 5,304
  • 5
  • 38
  • 96
  • What version of Xcode and what version of Swift are you using? – Sweeper Jun 15 '19 at 10:46
  • @Sweeper I'm using Xcode 10 and Swift 4.0, does it have to do with that?? – Vincenzo Jun 15 '19 at 11:31
  • @Vincenzo - it probably only happens when you use newer Swift 4.2 and paste it into a Swift 4.0 project... I had similar problems with same type of code, but only in my Swift 4.0 projects. – benc Aug 17 '22 at 23:16

3 Answers3

1

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.

Davydov Denis
  • 346
  • 1
  • 6
  • if you have a look at the picture you'll se that I'm trying it. At writing it it seems to be accepted, but at build it throws the error.. please look at the picture to see the 3 way I'm writing it – Vincenzo Jun 15 '19 at 12:21
  • Cleaning build folder or delete derived data folder didn't help. New build fails on that.. – Vincenzo Jun 15 '19 at 12:36
1

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

Vincenzo
  • 5,304
  • 5
  • 38
  • 96
0

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.)

benc
  • 1,381
  • 5
  • 31
  • 39
  • Thank you for your answer, dough the issue is two years old and Swift is well over version 4 your answer is well accepted as I'm sure this issue will present itself in similar scenarios, and your answer is well articulated. Thanks. – Vincenzo Aug 19 '22 at 06:15