2016-12-18 3 views
0

私は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) 
    } 

「プロフィール画像」と呼ばれるフィールドを持っている私の「ユーザーのクラスにアップロードできるようにしたいです。ここで

+0

私たちは読者ではありません。あなたの質問の内容を更新してください(コメントを投稿しないでください)。あなたはあなたが持っている問題の説明がないだけでコードを投稿することはできません。 – rmaddy

答えて

0

は、私が何かを助けるために願っています私の銀行

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 { 

     } 
    } 

に自分の画像をアップロードするために、このコードを使用して、私のコードです。

0

まず、parse.comから移行することについてのマイグレーション文書を見ることは、すぐに終了することが重要です。あなたの質問に今

http://parse.com/migration

。ここに、現在ログインしているユーザーのための迅速な例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") 

        }}) 


    } 
関連する問題