0

Im trying to implement a profile picture complete with upload button to firebase. I'm using swift 4.0 and im stuck on the syntax which is required for converting the jpeg image into data.

an error message is displayed providing the fix to the new replacement code, and following the instructions another error is played.

@IBAction func uploadButtonWasTapped(_ sender: Any) {
progressView.isHidden = false
let randomID = UUID.init().uuidString
let uploadRef = Storage.storage().reference(withPath:     
"images/(randomID).jpg")


 guard let imageData =    
 imageView.image?.jpegData(compressionQuality: 
 0.75) else { return }


 let uploadMetadata = StorageMetadata.init()
 uploadMetadata.contentType = "image/jpeg"

 let taskReference = uploadRef.putData(imageData, metadata: 
 uploadMetadata) { (downloadMetadata, error) in
   if let error = error {
 print("Oh no! Got and Error! \(error.localizedDescription)")
    return
        }
 print("Put is complete: \(String(describing: downloadMetadata))")
    }

the line I am having issued with is the

 'guard let imageData =   
 imageView.image?.jpegData(compressionQuality: 
 0.75) else { return }'

error received : 'jpegData(compressionQuality:)' has been renamed to
'UIImageJPEGRepresentation(::)'

code is changed to

   'guard let imageData =  
   imageView.image?.UIImageJPEGRepresentation(compressionQuality: 
   0.75) else { return }

enter image description hereerror received : Value of type 'UIImage' has no member
'UIImageJPEGRepresentation'

any ideas?

Erinballs
  • 3
  • 5
  • According to Apple's documentation (https://developer.apple.com/documentation/uikit/uiimage/1624115-jpegdata), how you wrote it first, is the correct way: `imageView.image?.jpegData(compressionQuality: 0.75)`. – Jeroen Oct 25 '19 at 06:44
  • Just on a side note, I'd consider changing your code formatting. It could be that StackOverflow messes it up. I find this to be useful: https://google.github.io/swift/ – Jeroen Oct 25 '19 at 06:48

2 Answers2

0

Use it like this, as your syntax is wrong. That's why you are getting this error.

guard let imageData = UIImageJPEGRepresentation(imageView.image, 0.75) else { return }
Jeroen
  • 2,011
  • 2
  • 26
  • 50
Hammer Class
  • 359
  • 1
  • 19
  • According to Apple's documentation (https://developer.apple.com/documentation/uikit/1624115-uiimagejpegrepresentation?language=objc), this is the Objective-C-like syntax. The Swift syntax (https://developer.apple.com/documentation/uikit/uiimage/1624115-jpegdata) is `imageView.image?.jpegData(compressionQuality: 0.75)` – Jeroen Oct 25 '19 at 06:46
  • It's not a Swift syntax change, but rather iOS. See also this: https://stackoverflow.com/a/51531204/2399348 So @Erinballs, what iOS versions are you supporting? – Jeroen Oct 25 '19 at 06:51
  • It's definitely swift syntax change you need to try it out by yourself.@JeroenJK – Hammer Class Oct 25 '19 at 07:07
  • I definitely did. Like I expected, this is the result: `'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'` - `Replace 'UIImageJPEGRepresentation(self.disclosureImage.image!, ' with 'self.disclosureImage.image!.jpegData(compressionQuality: '` This works though: `self.disclosureImage.image?.jpegData(compressionQuality: 1.0)` – Jeroen Oct 25 '19 at 07:16
  • I'm not trying to have a discussion here, I know I could be wrong as well. It's just that I can't seem to find out how your syntax can be used. I think a good first step is to find out the supported iOS versions for @Erinballs. – Jeroen Oct 25 '19 at 07:17
  • swift 4.0. appreciate the responses so far peoples. iOS 13.0 – Erinballs Oct 25 '19 at 08:36
  • For iOS 13.0 it's definitely `jpegData(compressionQuality: 1.0)`. – Jeroen Oct 25 '19 at 10:05
  • thank you @JeroenJK, I've entered your suggestion, my code is now 'guard let imageData = self.disclosureImage.image.jpegData(compressionQuality: 1.0) else { return }' however it is now displaying another error 'Value of type 'IAProfileViewController' has no member 'disclosureImage'' - this is the viewcontroller, do I have to add another class? – Erinballs Oct 25 '19 at 10:24
  • `disclosureImage` was a name from my own code ;-) See the answer I posted just now. – Jeroen Oct 25 '19 at 11:45
  • that makes sense :) turns out I'm actually using 12.2. (sorry for the confusion) i used the syntax you mention, which now seems to be accepted. The only issue I have is that 'nil' is found when unwrapping the value, which obviously caused the app to crash. guard let imageData = UIImageJPEGRepresentation(self.imageView.image!, 0.75) else { return } let imageView = UIImage(data: imageData) let storageRef = Storage.storage(url: "gs://*********-215716.appspot.com") let uploadRef = Storage.storage().reference(withPath: "images/\(randomID).jpg") – Erinballs Oct 27 '19 at 09:23
  • @Erinballs You are getting nil because your `imageView.image` is nil. make sure it has any image else you will get nil and your App will crash again. – Hammer Class Oct 28 '19 at 05:29
0

Change your code to be:

guard let imageDate = imageView.image?.jpegData(compressionQuality: 0.75) else
{ 
    return
}

UIImageJPEGRepresentation has been replaced with jpegData. See also the answer in this thread: https://stackoverflow.com/a/51531204/2399348

From Apple's documentation:

Although it seems like it was renamed going from Objective-C to Swift, it seems like it's only the case for iOS 12.0 and later. (See the previously mentioned SO thread). Since you state you're supporting iOS 13.0 and higher, you need to use the new syntax.

Jeroen
  • 2,011
  • 2
  • 26
  • 50
  • I just noticed the screenshot you attached in your question. Are you sure you're supporting only iOS 13.0 and higher? – Jeroen Oct 25 '19 at 11:49