web-dev-qa-db-ja.com

UIImagePickerControllerがアプリをクラッシュさせる| Swift3、Xcode8

FYI:Swiftが初めてなので、これは本当に簡単な問題かもしれませんが、私はそれを理解することができません。

iOSアプリの開発を開始(Swift) Xcode 8ベータ版を使用したチュートリアルとSwift 3。

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を表示せずにアプリがクラッシュします。

問題は、presentViewControllerではなく、新しいpresentにあると思いますSwift 3で導入されました


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
26
ARS

このキーを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プロジェクトです。

enter image description here

53
user6375148

Xcode 8では、新しいキーがプライバシーのために追加されました-

メディア、場所、写真ライブラリ、リマインダー、モーション、カレンダー、Bluetooth、HomeKit、カメラ、連絡先など。値はプライバシーの説明です。のためのアクセス。 「機能を機能させるには写真ライブラリを使用するためのアクセスが必要です」またはANY_YOUR_APP_RELATED_DESCRIPTION。

enter image description here

15
Bhoomi Jagani

カメラの使用にもカメラのキーと値が必要であることに注意してください。

Info.plistに含める必要があるカメラと写真ライブラリの両方に必要なキー:

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

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

プライバシーメッセージを追加しようとしましたが、それらはうまくいきませんでした。次に、画像要素自体の属性インスペクターを見て、以前はオフにされていた「User Interaction Enabled」ボックスをオンにしました。これを行った後、ピッカーは機能しました。それが誰かを助けることを願っています。

0
windkostya