Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

ios - UIImageJPEGRepresentation has been replaced by instance method UIImage.jpegData(compressionQuality:)

I've tried to upload a photo to Firebase but it's giving me this error. It was working before Xcode 10. I'm getting this error:

'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'

and I don't know how to use this function.

import UIKit
import Firebase

class SignUpViewController:UIViewController {
    @IBOutlet weak var profileImageView: UIImageView!
    @IBOutlet weak var tapToChangeProfileButton: UIButton!

    var continueButton:RoundedWhiteButton!
    var imagePicker:UIImagePickerController!

    override func viewDidLoad() {
        super.viewDidLoad()

          continueButton.addTarget(self, action: #selector(handleSignUp), for: 
          .touchUpInside)
        let imageTap = UITapGestureRecognizer(target: self, action: 
        #selector(openImagePicker))
        profileImageView.isUserInteractionEnabled = true
        profileImageView.addGestureRecognizer(imageTap)
        profileImageView.layer.cornerRadius = profileImageView.bounds.height / 2
        profileImageView.clipsToBounds = true

        imagePicker = UIImagePickerController()
        imagePicker.allowsEditing = true
        imagePicker.sourceType = .photoLibrary
        imagePicker.delegate = self
    }

    func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let storageRef = Storage.storage().reference().child("user/(uid)")

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

        let metaData = StorageMetadata()
        metaData.contentType = "image/jpg"

        storageRef.putData(imageData, metadata: metaData) { metaData, error in
            if error == nil, metaData != nil {
                if let url = metaData?.downloadURL() {
                    completion(url)
                } else {
                    completion(nil)
                }
                // success!
            } else {
                // failed
                completion(nil)
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error is telling you that as of iOS 12 the old UIImageJPEGRepresentation function has been replaced with the new jpegData method on UIImage.

Change:

let imageData = UIImageJPEGRepresentation(image, 0.75)

to:

let imageData = image.jpegData(compressionQuality: 0.75)

Similarly, the use of UIImagePNGRepresentation has been replaced with pngData().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...