简体   繁体   English

Swift 3中的UIImagePickerController

[英]UIImagePickerController in Swift 3

I've recently updated to Swift 3 in Xcode 8 using the developer beta. 我最近使用开发人员测试版更新了Xcode 8中的Swift 3。 I'm trying to implement Firebase Auth, and everything's going well. 我正在尝试实施Firebase Auth,一切进展顺利。

My Problem: 我的问题:

I'm trying to upload an image as a user's profile picture to a Firebase database. 我正在尝试将图像作为用户的个人资料图片上传到Firebase数据库。

I thought I could use the UIImagePickerController to do this, but when I do, I get a 我以为我可以使用UIImagePickerController来做到这一点,但是当我这样做时,我得到了一个

"Thread 7: Signal SIGABRT “主题7:信号SIGABRT

I know what this would normally indicate, but I checked, and my image does indeed print my test statement when I tap it. 我知道这通常表示什么,但我检查了,当我点击它时,我的图像确实打印了我的测试语句。

My failed method: 我失败的方法:

ViewController.swift ViewController.swift

import UIKit
import Firebase
import Photos

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

@IBOutlet weak var imageView: UIImageView!
@IBOutlet var emailTextField: UITextField!
@IBOutlet var passWordTextField: UITextField!
@IBOutlet var nameTextField: UITextField!

override func viewDidLoad() {
        super.viewDidLoad()

        if FIRAuth.auth()?.currentUser?.uid != nil {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let view = storyboard.instantiateViewController(withIdentifier: "ProfileViewController")
            self.present(view, animated: true, completion: nil)
        }

        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
        imageView.isUserInteractionEnabled = true

        self.emailTextField.delegate = self;
        self.passWordTextField.delegate = self;
        self.nameTextField.delegate = self;
    }

func handleSelectProfileImageView() {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.allowsEditing = true

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

I'm not sure yet if this is just a problem with Swift 3 and the developer Xcode 8 beta, or if I'm just doing it the wrong way for swift 3. 我不确定这是否只是Swift 3和开发人员Xcode 8 beta的问题,或者我只是对swift 3做错了。

EDIT: 编辑:

I'm using the GM Version of xcode8, but still get that same error. 我正在使用GM版的xcode8,但仍然会得到同样的错误。

截图

screenshot2

screenshot3

I think you need to add privacy - key for camara and photo library in your info.plist file as from xcode 8 . 我认为您需要添加privacy - key来自xcode 8 info.plist文件中的camara和照片库的privacy - key for example, 例如,

 Key : Privacy - Media Library Usage Description
 Value : YES 

 Key : Privacy - Photo Library Usage Description
 Value : YES 

 Key : Privacy - Camara Usage Description
 Value : YES 

here value is string not Boolean . 这里的值是string而非Boolean

so try this. 试试吧。

Check Apple documentation for more details! 查看Apple文档了解更多详情!

Reference : this so post 参考: 这个帖子

Follow the error message from xCode, and add key/value to Info.plist of application. 按照xCode的错误消息,将键/值添加到应用程序的Info.plist中。

For example, I got an error message from my developing app. 例如,我从开发的应用程序收到了一条错误消息。 It reminds me to add NSCameraUsageDescription to app's Info.plist. 它提醒我将NSCameraUsageDescription添加到应用程序的Info.plist中。


The error message from xCode 来自xCode的错误消息

This app has crashed because it attempted to access privacy-sensitive data without a usage description. 此应用程序已崩溃,因为它试图在没有使用说明的情况下访问隐私敏感数据。 The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data. 应用程序的Info.plist必须包含一个NSCameraUsageDescription键,其中包含一个字符串值,向用户解释应用程序如何使用此数据。


And MORE 和更多

Add key/value pair to Localizable.strings , it will give i18n support. key/value对添加到Localizable.strings ,它将提供i18n支持。 For example, I add a pair to Localizable.strings (English) . 例如,我将一对添加到Localizable.strings (English) It will give English description when ask user about camera permission. 当询问用户有关相机许可时,它会给出英文描述。

"NSCameraUsageDescription" = "We needs camera permission to capture image.";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM