【问题标题】:Saving and retrieving an image from parse in swift快速保存并从解析中检索图像
【发布时间】:2016-12-18 18:16:40
【问题描述】:

我有一个名为ProfilePage 的课程,我正在选择要显示为个人资料图片的图像。我已经使用 ImagePickerController 成功打开了图像。但是,我无法将此图像上传到我的 Parse.com 仪表板到我的“用户”类中。

我正在尝试将图像从 UIImagePicker 上传到 Parse.com。我遵循了一些教程并查看了上传文件的文档,但他们都将其添加到解析中的新“类”中。我希望能够上传到我的“用户”类中,其中有一个名为“个人资料图片”的字段

class ProfilePage: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    @IBOutlet weak var backButton: UIButton!
    @IBOutlet weak var profilePicture: UIImageView!
    @IBOutlet weak var editProfilePicture: UIButton!
    @IBOutlet weak var nameTag: UILabel!
    @IBOutlet weak var Bio: UILabel!
    @IBOutlet weak var saveProfileChanges: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        nameTag.text = ((PFUser.current()?.username)!)
    }

    @IBAction func didTapSaveProfileChanges(_ sender: Any) {
        PFUser.current()?.username = "Kane"

        do {
            try PFUser.current()?.saveInBackground(block: { (success, error) in
                if error != nil {
                    print("unable to save new username")
                }
                else {
                    print("new username saved")
                    self.viewDidLoad()
                }
            })
        } catch  {
            print("unable to save new username")
        }

    @IBAction func didTapBackButton(_ sender: Any) {
        self.performSegue(withIdentifier: "GoToHomePage", sender: self)
    }

    @IBAction func didTapChooseImage(_ sender: Any) {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self


        let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)

        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in

            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                imagePickerController.sourceType = .camera
                self.present(imagePickerController, animated: true, completion: nil)
            }
            else {
                print("There no is no camera available")
            }
        }))

        actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
            imagePickerController.sourceType = .photoLibrary
            self.present(imagePickerController, animated: true, completion: nil)
        }))

        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        self.present(actionSheet, animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        profilePicture.image = image
        picker.dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

如果有人有任何建议,将不胜感激。

【问题讨论】:

  • 我们不是读心术的人。请使用有关您的问题的详细信息更新您的问题(不要发布 cmets)。您不能只发布代码而不解释您遇到的问题。

标签: swift parse-platform uiimage


【解决方案1】:

这是我的代码,我使用此代码将图像上传到我的银行

    let profileImageData = UIImageJPEGRepresentation(photoProfile.image!,0.00001)

    var myPFObj = PFObject(className:"YouClass")

    let myFile = PFFile(name: "ProfilePic", data: profileImageData!)    
    myPFObj.setObject(myFile!, forKey: "profile_picture")

    myPFObj.saveInBackgroundWithBlock {
      (success: Bool, error: Error?) -> Void in
      if (success) {

      } else {

      }
    }

我希望能帮上忙。

【讨论】:

    【解决方案2】:

    首先,查看有关从 parse.com 迁移的迁移文档很重要,因为它即将关闭。

    http://parse.com/migration

    现在回答你的问题。这是 swift 3 中当前登录用户的示例。

    @IBOutlet weak var profilePicture: UIImageView!
    
    
    // ImagePicker delegate
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
    
            profilePicture.image = image
        }
        dismiss(animated: true, completion: nil)
    }
    
    
    @IBAction func updateProfileImage(_ sender: AnyObject) {
                    let userToUpdate = PFUser.current()!
    
    
                        // Save Avatar
                        if profilePicture.image != nil {
                            let imageData = UIImageJPEGRepresentation(avatarImg.image!, 0.5)
                            let imageFile = PFFile(name:"avatar.jpg", data:imageData!)
                            userToUpdate["profilePicture"] = imageFile
                        }
    
                        // Saving block
                        userToUpdate.saveInBackground(block: { (succ, error) in
                            if error == nil {
    
                                print("Your Profile has been updated!")
                            } else {
    
                                print("Failed")
    
                        }})
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多