使用 Parse (Swift 3) 注册用户时如何添加头像
Posted
技术标签:
【中文标题】使用 Parse (Swift 3) 注册用户时如何添加头像【英文标题】:How do I add an avatar when signing up a user with Parse (Swift 3) 【发布时间】:2017-01-03 17:33:41 【问题描述】:我有一个函数可以成功地将用户添加到我的 Parse 类的 User 表中,但我想在注册表单中添加一个头像。
事情的 stoyboard 方面工作正常,我可以从相机或照片库中选择图像并将其添加到我的 imageView (profilePic) 但是当我尝试将其添加到 signUpInBackground 方法时,它会使应用程序崩溃。
我是 ios 开发的新手,但熟悉其他编码语言,所以它并不完全是陌生的,我只是不知道我在这里缺少什么,或者是否无法在注册时添加图像?
救命!
let user = PFUser()
user.email = emailAddress.text
user.username = screenName.text
user.password = password.text
let image = self.profilePic.image
if image != nil
let imagedata = UIImagePNGRepresentation(image!)!
let file = PFFile(name: "image.png", data: imagedata)
user["profilePic"] = file
user.signUpInBackground(block: (success, error) in
if error != nil
// error code
else
// user logged in successfully
【问题讨论】:
【参考方案1】:这是你可以做的。我也为它添加了验证。我还创建了它,一旦成功注册,然后登录用户。如果你不想要它,你可以删除它。这是swift3
示例!
@IBOutlet weak var avatarImage: UIImageView!
@IBOutlet var emailAddress: UITextField!
@IBOutlet var password: UITextField!
// MARK: - UPLOAD AVATAR BUTTON
@IBAction func uploadAvatarButt(_ sender: AnyObject)
let alert = UIAlertController(title: APP_NAME,
message: "Select source",
preferredStyle: .alert)
let camera = UIAlertAction(title: "Take a picture", style: .default, handler: (action) -> Void in
if UIImagePickerController.isSourceTypeAvailable(.camera)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
)
let library = UIAlertAction(title: "Pick from library", style: .default, handler: (action) -> Void in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
)
// Cancel button
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: (action) -> Void in )
alert.addAction(camera)
alert.addAction(library)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
// ImagePicker delegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
avatarImage.image = image
dismiss(animated: true, completion: nil)
// MARK: - SIGNUP BUTTON
@IBAction func signupButt(_ sender: AnyObject)
if password.text == "" || emailAddress.text == "" || screenName.text == ""
//You can alert here , that fields need to be filled.
else
let userForSignUp = PFUser()
userForSignUp.username = screenName.text!.lowercased()
userForSignUp.password = password.text
userForSignUp.email = emailAddress.text
if avatarImage.image != nil
let imageData = UIImageJPEGRepresentation(avatarImage.image!, 0.8)
let imageFile = PFFile(name:"image.jpg", data:imageData!)
// profilePic needs to be the name of the col
userForSignUp["profilePic"] = imageFile
userForSignUp.signUpInBackground (succeeded, error) -> Void in
if error == nil
//Signup Success
PFUser.logInWithUsername(inBackground: self.screenName.text!, password:self.password.text!) (user, error) -> Void in
if error == nil
//Login Success
else
//Login Falied
// ERROR
else
//Signup Failed
【讨论】:
太棒了!非常感谢!我不想用你所有的代码替换我所有的代码,而是想逐行检查有什么不同,我唯一能发现不同的是你保存的是 Jpeg 而不是 Png - 所以我改变了它并且有效。看起来 IOS 模拟器中的默认图像,当保存为 PNG 时超过 10mb,这就是导致崩溃的原因!非常感谢您对此的帮助!以上是关于使用 Parse (Swift 3) 注册用户时如何添加头像的主要内容,如果未能解决你的问题,请参考以下文章
Swift - 立即解析 Facebook 登录默认为用户取消登录
在 iOS Swift 中实现方法时如何为闭包命名(语法问题)
如果在 Parse 中找到,则输入邮政编码切换视图控制器(Swift 和 XCode)