2017-08-29 149 views
-1

我試圖用UIImagePickerController顯示從照片庫中選取的圖像,但是當我選擇圖像時,沒有任何反應。從iOS照片庫中選擇的圖片不顯示在UIImageView中

我所做的只是從Apple's Swift tutorial開始,所以我猜這個代碼不應該是錯的。它在模擬器和真實設備中都不起作用。我嘗試了使用Google搜索時發現的所有建議。我的代碼:

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

「我做的是從蘋果公司的雨燕教程」不,這是不是在所有。您沒有按照教程中的說明操作。 – matt

回答

1

您的委託方法在按鈕操作範圍內。

您必須在selectImageFromPhotoLibrary作用域中編寫UIImagePickerControllerDelegate方法,並寫入正確的簽名。

就像這樣:

@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

是的,這是問題,錯誤的錯誤,謝謝@SalmanGhumsani –

相關問題