热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

创建具有未知类型的图像格式是一个错误...Swift3

如何解决《创建具有未知类型的图像格式是一个错误Swift3》经验,为你挑选了1个好方法。

我正在youtube上学习一些快速的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")
        }
}
}

bsm-2000.. 5

而不是你的代码:

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中的一个错误,如果选择器可以正确选择并返回图像,这意味着一切正常,只需忽略它.



1> bsm-2000..:

而不是你的代码:

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中的一个错误,如果选择器可以正确选择并返回图像,这意味着一切正常,只需忽略它.


推荐阅读
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • Gitlab接入公司内部单点登录的安装和配置教程
    本文介绍了如何将公司内部的Gitlab系统接入单点登录服务,并提供了安装和配置的详细教程。通过使用oauth2协议,将原有的各子系统的独立登录统一迁移至单点登录。文章包括Gitlab的安装环境、版本号、编辑配置文件的步骤,并解决了在迁移过程中可能遇到的问题。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • C语言自带的快排和二分查找
    Author🚹:CofCaiEmail✉️:cai.dongjunnexuslink.cnQQ😙:1664866311personalPage&#x ... [详细]
author-avatar
遇见你_天意_384
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有