Ios didFinishPickingMediaWithInfo未调用
我有一个Ios didFinishPickingMediaWithInfo未调用,ios,uiimagepickercontroller,Ios,Uiimagepickercontroller,我有一个UITableViewCell按钮来拍摄图像并将其放回单元格中。当我调用UIImagePickerController并拍摄图像时,它不会调用以下委托: func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject) 这是我的UITableViewController中的tak
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)}
您必须以这种方式使用该方法,因为您没有声明任何委托方法:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:[NSObject : AnyObject]) {
println("Calling")
}
Apple文档参考:委托方法现在称为
func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo:[String:AnyObject])
我看到您的
imagePickerController。委托
设置正确,因此我假设您在Swift 3中将didFinishPickingMediaWithInfo
代码意外放置在ViewController类之外,此委托方法现在被称为:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
区别在于:
我也有同样的问题,当我做出这些改变时,它起了作用。1。别忘了添加
UIImagePickerControllerDelegate、UINavigationControllerDelegate
2.在您的
viewDidLoad()
addpicker.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中使用以下方法:
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
}
对于SWIFT 4.2 当程序没有调用委托方法时,我也遇到了类似的问题
imagePicker.delegate=self
在viewDidLoad()方法中应该是而不是,而是在打开gallery的方法中。像这样:
func openGallery()
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, 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 此方法已重命名,现在应按如下方式调用。我遇到了这个委托方法没有调用的问题,但在这之后,我的问题解决了
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
}
对于Swift 5,我们略有更新
下面是我创建的一个单屏幕示例,用iOS 12.2来测试这一点
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)
}
}
如果UIImagePickerController()对象的allowsEditing属性设置为true,则应在委托方法中获取图像,如下所示: 如果
allowsdediting==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)
}
如果allowsdediting==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)
}
也许有时是电弧引起的? 我在下面为iOS14之前的版本声明了适配器变量(存在
UIImagePickerController
,或在控制器中为iOS14+声明了PHPickerViewController
)。
它对我有用
var adapter: PHPickerAdapter?
@objc func openGallery(recognizer: UITapGestureRecognizer) {
adapter = PHPickerAdapter()
adapter?.delegate = self
adapter?.showPhotoPicker(.photoLibrary, from: self)
}
你好,谢谢你的回答。我已经实现了委托方法,但在imagpepickercontroller完成后,不会调用此方法。您是否使用了我提到的委托方法,因为您的委托方法不正确,并且没有您声明的委托方法。
UIImage?无法转换为“UIImage”
从“Slice”强制转换而来(也称为“切片”)到不相关类型的“UIImage”总是失败