あなたのできることは次のとおりです。私はそれにもバリデーションを加えました。また、私はそれを作成した後、あなたがそれをしたくない場合は削除することができます。これは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
}}
}
}
これは素晴らしいです!どうもありがとうございます!すべてのコードをすべて自分のものに置き換えるのではなく、何が違うのかを行ごとにチェックしたかったのですが、PNGではなくJpegを保存していたということだけが違っていました。出来た。これは、IOSのシミュレータのデフォルト画像のように、PNG形式で保存された場合、クラッシュの原因となった10MB以上です!これであなたの助けをありがとう! – Dan
あなたはダン@ダンを歓迎します – Cliffordwh