我有一个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)}
答案 0 :(得分:39)
在 Swift 3 中,现在调用此委托方法:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
区别在于:
我有同样的问题,当我做出这些改变时,它起作用了。
答案 1 :(得分:15)
<强> 1。别忘了添加File
2.在UIImagePickerControllerDelegate,UINavigationControllerDelegate
添加viewDidLoad()
3.添加委托方法
picker.delegate = self
希望有所帮助:)
答案 2 :(得分:4)
快速4.2
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let chosenImage = info[.originalImage] as? UIImage{
//use image
}
}
答案 3 :(得分:2)
对Swift 4.2使用以下方法:
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
}
答案 4 :(得分:2)
对于 Swift 4.2
此方法已重命名,现在应按以下方式调用。我遇到了同样的问题,该委托方法未调用,但此后,我的问题解决了。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
}
答案 5 :(得分:1)
对于 SWIFT 4.2
当程序未调用委托方法时,我遇到了类似的问题。
在viewDidLoad()方法中,imagePicker.delegate = self
应该不,但是在打开图库的方法中。像这样:
func openGallery()
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
答案 6 :(得分:0)
您必须以这种方式使用该方法,因为没有声明的委托方法:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:[NSObject : AnyObject]) {
println("Calling")
}
答案 7 :(得分:0)
委托方法现在称为imagePickerController.delegate
我看到您的didFinishPickingMediaWithInfo
已正确设置,因此我假设您意外地将InsertOrReplace
代码放在ViewController类之外
答案 8 :(得分:0)
对于 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)
}
}
答案 9 :(得分:0)
如果您的UIImagePickerController()对象的allowEditing属性设置为true,则应在委托方法中将图像获取为:
如果allowsEditing == true
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[.editedImage] as? UIImage {
//do something with image
}
dismiss(animated: true, completion: nil)
}
如果allowsEditing == false
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[.originalImage] as? UIImage {
//do something with image
}
dismiss(animated: true, completion: nil)
}
答案 10 :(得分:0)
也许有时是由ARC引起的?
我在控制器中而不是在函数中声明了适配器的变量(对于以下iOS14之前的UIImagePickerController
,对于iOS 14+则当前的PHPickerViewController
)。
它对我有用。
var adapter: PHPickerAdapter?
@objc func openGallery(recognizer: UITapGestureRecognizer) {
adapter = PHPickerAdapter()
adapter?.delegate = self
adapter?.showPhotoPicker(.photoLibrary, from: self)
}
答案 11 :(得分:0)
在 Swift 5 中,我们需要使用:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {