Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 创建类型未知的图像格式时出错…Swift3_Ios_Firebase_Swift3 - Fatal编程技术网

Ios 创建类型未知的图像格式时出错…Swift3

Ios 创建类型未知的图像格式时出错…Swift3,ios,firebase,swift3,Ios,Firebase,Swift3,我正在youtube上学习一些swift 3的后续课程。下面我编写的代码用于创建用户帐户并将详细信息存储在Firebase数据库中。在测试时,我可以一直到提交注册表为止。然后,我收到以下错误: [Generic] Creating an image format with an unknown type is an error. Fatal Error: unexpectedly found nil whilst unwrapping an Optionional value. 我还在下面的代

我正在youtube上学习一些swift 3的后续课程。下面我编写的代码用于创建用户帐户并将详细信息存储在Firebase数据库中。在测试时,我可以一直到提交注册表为止。然后,我收到以下错误:

[Generic] Creating an image format with an unknown type is an error.
Fatal Error: unexpectedly found nil whilst unwrapping an Optionional value.
我还在下面的代码块中突出显示了以下行:

exc_bad_instruction(code=exc_i386_invop subcode=0x0)
下面是我的代码。我已经强调了抛出异常的位置。任何帮助都将不胜感激

import UIKit
import Firebase

class Signup_ViewController: UIViewController, 
UIImagePickerControllerDelegate, UINavigationControllerDelegate {

// Input data fields for signup form
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var nameField: UITextField!

// Password data field for signup form
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var confirmPasswordField: UITextField!

//  Next button for signup form (Hidden by default)
@IBOutlet weak var nextBtn: UIButton!

let picker = UIImagePickerController()
var userStorage: FIRStorageReference!
var ref: FIRDatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()
    picker.delegate = self

    let storage = FIRStorage.storage().reference(forURL: "XXXXXXXXXXXXXXXXXXXX") // Defines URL for Firebase storage container

    ref = FIRDatabase.database().reference()
    userStorage = storage.child("users") // Folder on Firebase storage
}

//  Image for signup form - user profile image
@IBOutlet weak var imgView: UIImageView!

// Action for when user presses the "Select profile picture" button
@IBAction func selectProfileImagePress(_ sender: Any) {

    picker.allowsEditing = true // Enables user to edit photo
    picker.sourceType = .photoLibrary // Enables user to pick photo from photo library

    present(picker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        self.imgView.image = image // Checks image selected exists
        nextBtn.isHidden = false // Unhides "Next" button once image has been picked
    }
    self.dismiss(animated: true, completion: nil)
}
// Action for when the "Next" button is pressed
@IBAction func nextPressed(_ sender: Any) {
    guard usernameField.text != "", nameField.text != "", emailField.text != "", passwordField.text != "", confirmPasswordField.text != "" else { return }
    if passwordField.text == confirmPasswordField.text {  // Checks password and confirm password match <---- Error highlights this line when the app crashes out.
           FIRAuth.auth()?.createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
            if let error = error {
                print(error.localizedDescription)
            }

            if let user = user {

                let changeRequest = FIRAuth.auth()!.currentUser!.profileChangeRequest()
                changeRequest.displayName = self.nameField.text!
                changeRequest.commitChanges(completion: nil)

                let imageRef = self.userStorage.child("\(user.uid).jpg") // Creates JPG file for user uploading (user.uid is variable for specific user)

                let data = UIImageJPEGRepresentation(self.imgView.image!, 0.5) // Prepares user profile picture to be sent to Firebase.  Applies 0.5 compression to image.

                let uploadTask = imageRef.put(data!, metadata: nil, completion: { (metadata, err) in
                    if err != nil {
                        print(err!.localizedDescription)
                    }

                    imageRef.downloadURL(completion: { (url, er) in
                        if er != nil {
                            print(er!.localizedDescription)
                        }

                        if let url = url {
                            let userInfo: [String : Any] = ["uid" : user.uid,
                                                            "username" : self.usernameField.text!,
                                                            "name" : self.nameField.text!,
                                                            "urltoImage" : url.absoluteString]

                        self.ref.child("users").child(user.uid).setValue(userInfo)

                            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "userVC")

                            self.present(vc, animated: true, completion: nil)
                        }
                    })
                })
                uploadTask.resume()
            }
           })

        } else {
        print ("Password does not match")
        }
}
}
导入UIKit
进口火基
类注册\u ViewController:UIViewController,
UIImagePickerControllerDelegate,UINavigationControllerDelegate{
//为注册表单输入数据字段
@ibvar usernameField:UITextField!
@IBOutlet弱var emailField:UITextField!
@IBVAR名称字段:UITextField!
//注册表单的密码数据字段
@IBOUTLE弱var密码字段:UITextField!
@IBOutlet弱var confirmPasswordField:UITextField!
//注册表单的“下一步”按钮(默认情况下隐藏)
@IBOUTLE弱var nextBtn:UIButton!
让picker=UIImagePickerController()
var userStorage:FIRStorageReference!
var ref:FIRDatabaseReference!
重写func viewDidLoad(){
super.viewDidLoad()
picker.delegate=self
让storage=FIRStorage.storage().reference(forURL:“xxxxxxxxxxxxxxxxxxxxx”)//定义Firebase存储容器的URL
ref=FIRDatabase.database().reference()
userStorage=storage.child(“users”)//Firebase存储上的文件夹
}
//注册表单的图像-用户配置文件图像
@IBVAR imgView:UIImageView!
//用户按下“选择配置文件图片”按钮时的操作
@iAction func selectProfileImagePress(\发送方:任意){
picker.allowsdediting=true//允许用户编辑照片
picker.sourceType=.photoLibrary//允许用户从照片库中拾取照片
当前(选择器,动画:真,完成:无)
}
func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo:[字符串:任意]){
如果让image=info[UIImagePickerControllerEditedImage]作为?UIImage{
self.imgView.image=图像//检查所选图像是否存在
nextBtn.isHidden=false//拾取图像后取消隐藏“下一步”按钮
}
self.disclose(动画:true,完成:nil)
}
//按下“下一步”按钮时的操作
@iAction func nextPressed(\发送方:任意){
guard usernameField.text!=“”,nameField.text!=“”,emailField.text!=“”,passwordField.text!=“”,confirmPasswordField.text!=“”,否则{return}
如果passwordField.text==confirmPasswordField.text{//检查密码并确认密码匹配,而不是您的代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
    self.imgView.image = image // Checks image selected exists
    nextBtn.isHidden = false // Unhides "Next" button once image has been picked
}
self.dismiss(animated: true, completion: nil)
}

用这个

/// what app will do when user choose & complete the selection image :
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    /// chcek if you can return edited image that user choose it if user already edit it(crop it), return it as image
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {

        /// if user update it and already got it , just return it to 'self.imgView.image'
        self.imgView.image = editedImage

        /// else if you could't find the edited image that means user select original image same is it without editing . 
    } else if let orginalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        /// if user update it and already got it , just return it to 'self.imgView.image'.
        self.imgView.image = orginalImage
    } 
        else { print ("error") }

    /// if the request successfully done just dismiss 
    picker.dismiss(animated: true, completion: nil)

}
对于此错误:

创建类型未知的图像格式时出错…Swift3

这是xcode中的一个bug,如果picker能够正确地选择并返回图像,这意味着一切正常,那么就忽略它。

而不是您的代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
    self.imgView.image = image // Checks image selected exists
    nextBtn.isHidden = false // Unhides "Next" button once image has been picked
}
self.dismiss(animated: true, completion: nil)
}

用这个

/// what app will do when user choose & complete the selection image :
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    /// chcek if you can return edited image that user choose it if user already edit it(crop it), return it as image
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {

        /// if user update it and already got it , just return it to 'self.imgView.image'
        self.imgView.image = editedImage

        /// else if you could't find the edited image that means user select original image same is it without editing . 
    } else if let orginalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

        /// if user update it and already got it , just return it to 'self.imgView.image'.
        self.imgView.image = orginalImage
    } 
        else { print ("error") }

    /// if the request successfully done just dismiss 
    picker.dismiss(animated: true, completion: nil)

}
对于此错误:

创建类型未知的图像格式时出错…Swift3


这是xcode中的一个错误,如果picker能够正确选择并返回图像,这意味着一切都正常,那么就忽略它。

您是否检查过脚本中的passwordField和confirmPasswordField是否正确连接?这将解释该行中的“意外发现零”错误。我无法对“创建图像”进行评论。。。错误-这对我来说是新的。嗨,我已经检查了confirmPasswordField,甚至重新链接。仍然得到错误。我已经解决了创建图像的错误。还有passwordField?你仍然在同一行上得到相同的意外发现零错误吗?该行上只有两个选项,其中一个是零-这就是错误所在正在告诉你。你检查过密码字段和confirmPasswordField在情节提要中是否正确连接了吗?这可以解释这一行的“意外发现零”错误。我无法对“创建图像…”错误发表评论-这对我来说是新错误。嗨,我检查过confirmPasswordField,甚至重新链接了。仍然收到错误。我已经解决了创建图像的错误。密码字段呢?你还在同一行上得到同样的意外发现的零错误吗?这行上只有两个选项,其中一个是零-这就是错误告诉你的。嗨,这肯定解决了我的一个问题。现在我只剩下一个,我希望能解决由第一个海报评论。谢谢你的帮助。很高兴看到这一点。亲爱的,我已经回答你的“创建未知类型的图像格式是一个错误”这是xcode中的一个bug,如果picker能够正确地选择并返回图像,这意味着一切都正常,请忽略它。“嗨,这肯定解决了我的一个问题。现在我还有一个问题,我希望通过第一条海报评论来解决。谢谢你的帮助。很高兴看到这一点。亲爱的,我已经回答了你的问题。”“创建一个未知类型的图像格式是一个错误”这是xcode中的一个错误,如果picker能够正确地选择并返回图像,这意味着一切正常,就忽略它