skip to Main Content

Value of type 'UIImageView' has no member 'jpegData'

Same code is working perfectly in another view controller.Help me Out to get rid of this problem. Here Is the Code….

import UIKit
import Firebase

class SettingTableViewController: UITableViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {

    @IBOutlet weak var profileImgView: UIImageView!
    @IBOutlet weak var usernameTexfield: UITextField!
    @IBOutlet weak var emailTextfield: UITextField!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Edit Profile"
        fetchCurrentUser()
    }
    func fetchCurrentUser() {
        Api.User.observeCurrentUser { (user) in
            self.usernameTexfield.text = user.username
            self.emailTextfield.text = user.email
            if let profileUrl = URL(string: user.profileImageUrl!) {
                self.profileImgView.sd_setImage(with: profileUrl)
            }
            
        }
    }
    @IBAction func SaveButton_TouchUpInside(_ sender: Any) {

        if let profileImg = self.profileImgView, let imageData = profileImg.jpegData(compressionQuality: 0.1) {
            AuthService.updateUserInfo(username: usernameTexfield.text!, email: emailTextfield.text!, imageData: imageData, onSuccess: {
                ProgressHUD.showSuccess("Success")
            }, onError: { (errorMaessage) in
                ProgressHUD.showError(errorMaessage)
            })
        }
    }
    
    
    @IBAction func LogOutButton_TouchUpinside(_ sender: Any) {
    }
    @IBAction func EditProfileBtn_TouchUpInside(_ sender: Any) {
        let pickercontroller = UIImagePickerController()
        pickercontroller.delegate = self
        present(pickercontroller, animated: true, completion: nil)
    }
}
extension SettingTableViewController {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print("did finish picking media")
        if let image = info["UIImagePickerControllerOrignalImage"]
            as? UIImage{
            profileImgView.image =  image
        }
        dismiss(animated: true, completion: nil)
    }

}


4

Answers


  1. Chosen as BEST ANSWER

    This Really works.

    @M.Ali @TaimoorArif After using Image property The Build Succeed, can you guys check the Edit profile button's code why the image is not uploading. I think there is something that I am missing.


  2. Make sure to use the image property of the profileImgView not the profileImgView directly.

    So Replace this line
    if let profileImg = self.profileImgView, let imageData = profileImg.jpegData(compressionQuality: 0.1) {

    with this

    if let profileImg = self.profileImgView.image, let imageData = profileImg.jpegData(compressionQuality: 0.1) {

    Login or Signup to reply.
  3. You are only setting profileImgView to profileImg. You have to set profileImageView.image to get this work properly.

    Try this code in Save button action:

    @IBAction func SaveButton_TouchUpInside(_ sender: Any) {
            if let profileImg = self.profileImgView.image, let imageData = profileImg.jpegData(compressionQuality: 0.1) {
                AuthService.updateUserInfo(username: usernameTexfield.text!, email: emailTextfield.text!, imageData: imageData, onSuccess: {
                    ProgressHUD.showSuccess("Success")
                }, onError: { (errorMaessage) in
                    ProgressHUD.showError(errorMaessage)
                })
            }
        }
    

    Hope this works.

    Login or Signup to reply.
  4. Take a variable, let:

    var imageDataURL = ""

    Now in the delegate function of imagePickerController you have to do:

    extension SettingTableViewController {
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            print("did finish picking media")
            if let image = info["UIImagePickerControllerOrignalImage"]
                as? UIImage{
    
            profileImgView.image =  image 
            
            guard let reducedImage = image.jpegData(compressionQuality : 0.0) else {print("image not compressed");return}
            let CompressedImg = reducedImage.base64EncodedString()
            self.imageDataURL = CompressedImg
                
            }
            dismiss(animated: true, completion: nil)
        }
    }
    

    Now in SaveButton action do this:

    @IBAction func SaveButton_TouchUpInside(_ sender: Any) {
     AuthService.updateUserInfo(username: usernameTexfield.text!, email: emailTextfield.text!, imageData: self.imageDataURL, onSuccess: {
                    ProgressHUD.showSuccess("Success")
                }, onError: { (errorMaessage) in
                    ProgressHUD.showError(errorMaessage)
                })
            }
    

    Hope this works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search