微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UIImagePickerController使应用程序崩溃| Swift3,Xcode8

如何解决UIImagePickerController使应用程序崩溃| Swift3,Xcode8

将此密钥添加到您的中 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]

That’s it, Clean & Run the project.

解决方法

仅供参考:我是Swift的新手,所以这可能是一个非常简单的问题,但我
无法弄清楚。

我一直在关注 使用Xcode 8 beta和Swift 3 的《开始开发iOS应用程序
(Swift)
教程》。

我将Tap Gesture Recognizer附加到了Image View,然后
在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,completion: nil)
    }

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


}

Console Output

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,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,privacy_setting: 0
2016-06-20 17:08:20.769881 FoodTracker[33322:696091] subsystem: com.apple.FrontBoard,category: Common,privacy_setting: 0
2016-06-20 17:08:22.007665 FoodTracker[33322:695971] subsystem: com.apple.UIKit,category: Touch,privacy_setting: 0
2016-06-20 17:08:22.009799 FoodTracker[33322:695971] subsystem: com.apple.UIKit,category: Gesture,privacy_setting: 0
2016-06-20 17:08:22.012973 FoodTracker[33322:695971] subsystem: com.apple.UIKit,category: GestureEnvironment,privacy_setting: 0
2016-06-20 17:08:22.013820 FoodTracker[33322:695971] subsystem: com.apple.UIKit,category: GestureExclusion,privacy_setting: 0
2016-06-20 17:08:23.061815 FoodTracker[33322:695971] subsystem: com.apple.photos,category: Generic,privacy_setting: 0

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。