我有一个UITableViewCell按钮来拍摄图像并将其放回单元格中。当我调用UIImagePickerController并获取图像时,它不会调用以下委托
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject)
这是我在UITableViewController中的takePhotoFunction:
@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)}
在Swift 3中,现在调用此委托方法:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
不同之处是:
我有同样的问题,当我做出这些改变时,它起作用了。
1.别忘了添加UIImagePickerControllerDelegate,UINavigationControllerDelegate
2.在你的viewDidLoad()
添加picker.delegate = self
3.添加委托方法
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)
}
希望能帮助到你 :)
对于swift 4.2
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let chosenImage = info[.originalImage] as? UIImage{
//use image
}
}
使用以下Swift 4.2方法:
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
}
对于Swift 4.2
此方法已重命名,现在应调用如下。我有同样的问题,这个委托方法没有调用,但在此之后,我的问题解决了。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
}
对于SWIFT 4.2
当程序没有调用委托方法时,我遇到了类似的问题。
imagePicker.delegate = self
应该不在viewDidLoad()方法中,而是在打开图库的方法中。像这样:
func openGallery()
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
您必须以这种方式使用该方法,因为没有您声明的委托方法:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:[NSObject : AnyObject]) {
println("Calling")
}
委托方法现在称为func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
我看到你的imagePickerController.delegate
设置正确所以我假设你意外地将didFinishPickingMediaWithInfo
代码放在ViewController类之外
有了Swift 5,我们稍作更新。
这是我创建的单屏幕示例,用于测试iOS 12.2
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)
}
}