Ich habe eine UITableViewCell-Schaltfläche, um ein Bild aufzunehmen und es wieder in die Zelle einzufügen. Wenn ich den UIImagePickerController anrufe und das Abbild nehme, ruft er den folgenden Delegaten nicht auf
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject)
Das ist meine takePhotoFunction im UITableViewController:
@IBAction func takePhoto(sender: AnyObject) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
let actionSheet = UIAlertController(title: "Choose image souruce", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Take Image", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)}
In Swift 3 heißt diese Delegatmethode jetzt:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
Die Unterschiede sind:
Ich hatte das gleiche Problem und als ich diese Änderungen vorgenommen habe, hat es funktioniert.
1. Vergessen Sie nicht, UIImagePickerControllerDelegate,UINavigationControllerDelegate
hinzuzufügen
2. Fügen Sie in Ihrer viewDidLoad()
picker.delegate = self
3. Fügen Sie die Delegat-Methoden hinzu
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
userImageView.contentMode = .scaleAspectFit
userImageView.image = chosenImage
dismiss(animated:true, completion: nil)
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {self.dismiss(animated: true, completion: nil)
}
Ich hoffe es hilft :)
Für Swift 4.2
Diese Methode wurde umbenannt und sollte nun wie folgt aufgerufen werden. Ich hatte das gleiche Problem, das diese Delegat-Methode nicht aufrief, aber danach wurde mein Problem gelöst.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
}
Für Swift 4.2
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let chosenImage = info[.originalImage] as? UIImage{
//use image
}
}
Verwenden Sie die folgende Methode für Swift 4.2:
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
}
für Swift 4.2
Ich hatte ein ähnliches Problem, als Delegat-Methoden nicht vom Programm aufgerufen wurden.
Der imagePicker.delegate = self
sollteNICHTin der viewDidLoad () - Methode sein, aber in der Methode, die die Galerie öffnet. So was:
func openGallery()
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
Die Delegatmethode heißt jetzt func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
Ich sehe, dass Ihr imagePickerController.delegate
richtig eingestellt ist, sodass ich davon ausgehen würde, dass Sie den didFinishPickingMediaWithInfo
-Code aus Versehen außerhalb Ihrer ViewController-Klasse platziert haben
Sie müssen diese Methode auf diese Weise verwenden, da es keine Delegate-Methode gibt, die Sie deklariert haben:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:[NSObject : AnyObject]) {
println("Calling")
}
Referenz für Apple Document: https://developer.Apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html#//Apple_ref/ occ/intfm/UIImagePickerControllerDelegate/imagePickerController: didFinishPickingMediaWithInfo :
Mit dem Swift 5 haben wir ein kleines Update.
Hier ist ein Beispiel für einen einzelnen Bildschirm, das ich erstellt habe, um dies mit iOS 12.2 zu testen
import UIKit
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: Open Photos Galley Button Action method
@IBAction func openPhotosGallery(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
}
//MARK: ImagePicker Controller Delegate methods
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let chosenImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
imageView.image = chosenImage
dismiss(animated:true, completion: nil)
}
}