2017-08-29 12 views
-1

UIImagePickerControllerを使用してフォトライブラリから選択した画像を表示しようとしていますが、画像を選択しても何も起こりません。iOS PhotoLibraryから選択した画像がUIImageViewに表示されない

私がやっているのはApple's Swift tutorialなので、コードは間違ってはいけないと思います。シミュレータや実​​機では動作しません。私はグーグルで見つけたすべての提案を試みました。私のコード:

import UIKit 

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

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

override func viewDidLoad() { 
    super.viewDidLoad() 
    nameTextField.delegate = self 
} 

//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) 

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

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

     // The info dictionary may contain multiple representations of the image. You want to use the original. 
     guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { 
      fatalError("Expected a dictionary containing an image, but was provided the following: \(info)") 
     } 

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

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

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

//MARK: UITextFieldDelegate 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    textField.resignFirstResponder() 
    return true 
} 

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

私は、必要に応じてさらに詳しい情報を提供することができます。前もって感謝します。

+1

いや、それがすべてではない「私がやっているすべては、Appleのスウィフトのチュートリアルからです」。チュートリアルの指示に従わなかった。 – matt

答えて

1

デリゲートメソッドはボタンアクションスコープにあります。

UIImagePickerControllerDelegateメソッドをselectImageFromPhotoLibraryアクションスコープの外に書いて、適切な署名で書き込む必要があります。ちょうどそのような

@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) 


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

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

     // The info dictionary may contain multiple representations of the image. You want to use the original. 
     guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { 
      fatalError("Expected a dictionary containing an image, but was provided the following: \(info)") 
     } 

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

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

これは半分です。デリゲートメソッドでは、メソッドのシグネチャはまだ間違っています。 – rmaddy

+0

@rmaddy思い出してくれてありがとう、私はそれを更新します。 –

+0

はい、これは問題でした、悪い間違い、ありがとう@サルマングムサニ –

関連する問題