UIImagePickerController 崩溃应用程序 |斯威夫特3,Xcode8

Posted

技术标签:

【中文标题】UIImagePickerController 崩溃应用程序 |斯威夫特3,Xcode8【英文标题】:UIImagePickerController crashes app | Swift3, Xcode8 【发布时间】:2016-06-20 14:53:19 【问题描述】:

仅供参考:我是 Swift 新手,所以这可能是一个非常简单的问题,但我就是想不通。

我一直在学习使用 Xcode 8 beta 和 Swift 3 的 Start Developing ios Apps (Swift) 教程。

我将 Tap Gesture Recognizer 附加到图像视图,然后在 ViewController.swift 中添加此操作:

@IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) 
        // Hide the keyboard.
        nameTextField.resignFirstResponder()

        // UIImagePickerController is a view controller that lets a user pick media from their photo library.
        let imagePickerController = UIImagePickerController()

        // Only allow photos to be picked, not taken.
        imagePickerController.sourceType = .photoLibrary

        // Make sure ViewController is notified when the user picks an image.
        imagePickerController.delegate = self

        present(imagePickerController, animated: true, completion: nil)
    

当通过点击图像视图调用 selectImageFromPhotLibrary 操作时,应用程序崩溃而不显示 ImagePicker。

我猜问题在于新的 present 而不是 Swift 3 中引入的 presentViewController


ViewController.swift
import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

    // MARK: Properties
    @IBOutlet weak var mealNameLabel: UILabel!
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var photoImageView: UIImageView!


    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //  Handle the text  field´s user input through delegate callbacks
        nameTextField.delegate = self
    

    // MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool 
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    

    func textFieldDidEndEditing(_ textField: UITextField) 
        mealNameLabel.text = textField.text
    

    // MARK: UIImagePickerControllerDelegate
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
        // Dismiss the picker if the user canceled.
        dismiss(animated: true, completion: nil)
    

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
        // The info dictionary contains multiple representations of the image, and this uses the original.
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage

        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
    


    // MARK: Actions

    @IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) 
        // Hide the keyboard.
        nameTextField.resignFirstResponder()

        // UIImagePickerController is a view controller that lets a user pick media from their photo library.
        let imagePickerController = UIImagePickerController()

        // Only allow photos to be picked, not taken.
        imagePickerController.sourceType = .photoLibrary

        // Make sure ViewController is notified when the user picks an image.
        imagePickerController.delegate = self

        present(imagePickerController, animated: true, completion: nil)
    

    @IBAction func setDefaultLabelText(_ sender: UIButton) 
        mealNameLabel.text = "Default Text"
    




控制台输出
2016-06-20 17:08:20.568093 FoodTracker[33322:696094] bundleid: com.armin.FoodTracker, enable_level: 0, persist_level: 0, propagate_with_activity: 0
2016-06-20 17:08:20.569458 FoodTracker[33322:696094] subsystem: com.apple.UIKit, category: HIDEvents, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-06-20 17:08:20.604909 FoodTracker[33322:696090] Created DB, header sequence number = 288
2016-06-20 17:08:20.668341 FoodTracker[33322:696090] Created DB, header sequence number = 288
2016-06-20 17:08:20.743143 FoodTracker[33322:696090] subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-06-20 17:08:20.769881 FoodTracker[33322:696091] subsystem: com.apple.FrontBoard, category: Common, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-06-20 17:08:22.007665 FoodTracker[33322:695971] subsystem: com.apple.UIKit, category: Touch, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-06-20 17:08:22.009799 FoodTracker[33322:695971] subsystem: com.apple.UIKit, category: Gesture, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-06-20 17:08:22.012973 FoodTracker[33322:695971] subsystem: com.apple.UIKit, category: GestureEnvironment, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-06-20 17:08:22.013820 FoodTracker[33322:695971] subsystem: com.apple.UIKit, category: GestureExclusion, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-06-20 17:08:23.061815 FoodTracker[33322:695971] subsystem: com.apple.photos, category: Generic, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0

【问题讨论】:

你会认为在发布错误时,发布错误会很有用。 你调用了错误的方法。你应该这样调用:self.presentViewController(imagePickerController, animated: true, completion: nil) @AMomchilov 没有错误消息。该应用程序刚刚关闭/崩溃 @AMomchilov 在问题中添加了控制台输出 self.presentViewController(imagePickerController, animated: true, completion: nil) 是教学材料中指定的旧 Swift 语法的一个示例。当 Xcode 更新到版本 8.0 (8A218a) 时,它会提示用户将它找到的任何旧 Swift 语法更新到 Apple Swift 版本 3.0 (swiftlang-800.0.46.2 clang-800.0.38)。上面显示的 ViewController.swift 代码使用了新的语法。 【参考方案1】:

将此密钥添加到您的info.plist

Key : Privacy - Photo Library Usage Description [ NSPhotoLibraryUsageDescription ]
String Value : We need access to your camera roll and photo library, so that we can do operations on it. [ Customise it in your own way] 

就是这样,Clean & Run 项目。

【讨论】:

您没有将值设置为YES。您将其设置为向用户显示的短消息。 @rmaddy 是的,他们已经在新的 Xcode 中更改了它。我修改了答案。 好收获。我会推荐 filing a bug to Apple 关于该教程,以便您可以按照它进行修复。 此外,您应该只需要此项目的“照片库使用说明”,而不需要“媒体库使用说明”。后者用于使用 MediaPlayer 等框架访问音乐和其他 iTunes 同步内容,本教程项目不这样做。 @rickster 是的,你是对的。我忘记了这一点,我只是把它放在那里以给出关于这些新键的整体概念。感谢您指出。【参考方案2】:

在 xcode 8 中添加了新密钥 Privacy For -

媒体、位置、照片库、提醒、运动、日历、蓝牙、HomeKit、相机、联系人 等。值是您将访问的隐私的描述。例如。 “我们需要使用照片库才能使功能正常工作”或 ANY_YOUR_APP_RELATED_DESCRIPTION。

【讨论】:

tnx,帮我可视化 为什么值设置为“YES”?它们应该是向用户显示的短消息。【参考方案3】:

还注意到相机使用所需的相机键和值。

需要在 info.plist 中的相机和照片库所需的密钥:

<key>NSCameraUsageDescription</key>
<string>Access needed to use your camera.</string>

<key>NSPhotoLibraryUsageDescription</key>
<string>Access needed to photo gallery.</string>

【讨论】:

【参考方案4】:

我尝试添加隐私消息,但这些对我不起作用。然后我查看了图像元素本身的属性检查器,并选中了以前未选中的框“启用用户交互”。完成此操作后,选择器开始工作。希望它可以帮助某人。

【讨论】:

对我来说,解雇不起作用。当我单击“使用照片”按钮时,相机会一次又一次地弹出。有没有办法让它工作?

以上是关于UIImagePickerController 崩溃应用程序 |斯威夫特3,Xcode8的主要内容,如果未能解决你的问题,请参考以下文章

UIImagePickerController 不是全屏

UIImagePickerController、自定义 UIButton 和 AutoLayout

UIImagePickerController 实况照片

OCMock 模拟 UIImagePickerController

UIImagePickerController 图像没有改变

无法弹出 UIImagePickerController