没有为 UIImagePickerControllerDelegate 调用 didFinishPickingMediaWithInfo
didFinishPickingMediaWithInfo not being called for UIImagePickerControllerDelegate
我正在尝试向我的 iOS 应用添加图片上传功能。我有一个函数 selectPicture(),当用户点击上传按钮时会调用该函数,然后弹出一个警告,允许用户在相机和图库之间进行选择。
func selectPicture() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
let alertController = UIAlertController(title: "Image Source", message: "Please choose your image source", preferredStyle: UIAlertControllerStyle.alert)
let camera_action = UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) {
(_: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
print ("Camera Selected")
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(imagePicker, animated: true, completion: nil)
}
}
let photoLibrary_action = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.default) {
(_: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
print ("Photo Library Selected")
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
imagePicker.navigationBar.titleTextAttributes = self.navbarAttributes
imagePicker.navigationBar.tintColor = UIColor.white
imagePicker.navigationBar.isTranslucent = false
imagePicker.navigationBar.barTintColor = PING_ORANGE
imagePicker.navigationController?.title = "Pick Image"
imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(imagePicker, animated: true, completion: nil)
}
}
let cancel_action = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
(_: UIAlertAction) -> Void in
print ("Cancel Selected")
}
alertController.addAction(camera_action)
alertController.addAction(photoLibrary_action)
alertController.addAction(cancel_action)
self.present(alertController, animated: true, completion: nil)
}
到目前为止一切正常,但是当我 select 图像时,委托方法没有被调用。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any], editingInfo: [AnyHashable: Any]!) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
print("Image Print")
print(image)
if image != nil {
print ("INSIDE IMAGE DID FINISH PICKING")
dismiss(animated: false) {
self.uploadProfileImage(image: image!)
}
} else {
dismiss(animated: false) {
}
}
}
我是 swift 的新手,所以我可能遗漏了一些东西。谁能帮我解决这个问题?
谢谢。
您可能使用的是旧密码。你的 method signature 是错误的。应该是:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
print("Image Print")
print(image)
dismiss(animated: false) {
self.uploadProfileImage(image: image)
}
} else {
dismiss(animated: false)
}
}
我使用以下方法解决了这个问题:
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
dismiss(animated: false) {
self.uploadProfileImage(image: image)
}
}
else {
dismiss(animated: false) {
}
}
}
我正在尝试向我的 iOS 应用添加图片上传功能。我有一个函数 selectPicture(),当用户点击上传按钮时会调用该函数,然后弹出一个警告,允许用户在相机和图库之间进行选择。
func selectPicture() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
let alertController = UIAlertController(title: "Image Source", message: "Please choose your image source", preferredStyle: UIAlertControllerStyle.alert)
let camera_action = UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) {
(_: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
print ("Camera Selected")
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(imagePicker, animated: true, completion: nil)
}
}
let photoLibrary_action = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.default) {
(_: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
print ("Photo Library Selected")
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
imagePicker.navigationBar.titleTextAttributes = self.navbarAttributes
imagePicker.navigationBar.tintColor = UIColor.white
imagePicker.navigationBar.isTranslucent = false
imagePicker.navigationBar.barTintColor = PING_ORANGE
imagePicker.navigationController?.title = "Pick Image"
imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(imagePicker, animated: true, completion: nil)
}
}
let cancel_action = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
(_: UIAlertAction) -> Void in
print ("Cancel Selected")
}
alertController.addAction(camera_action)
alertController.addAction(photoLibrary_action)
alertController.addAction(cancel_action)
self.present(alertController, animated: true, completion: nil)
}
到目前为止一切正常,但是当我 select 图像时,委托方法没有被调用。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any], editingInfo: [AnyHashable: Any]!) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
print("Image Print")
print(image)
if image != nil {
print ("INSIDE IMAGE DID FINISH PICKING")
dismiss(animated: false) {
self.uploadProfileImage(image: image!)
}
} else {
dismiss(animated: false) {
}
}
}
我是 swift 的新手,所以我可能遗漏了一些东西。谁能帮我解决这个问题?
谢谢。
您可能使用的是旧密码。你的 method signature 是错误的。应该是:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
print("Image Print")
print(image)
dismiss(animated: false) {
self.uploadProfileImage(image: image)
}
} else {
dismiss(animated: false)
}
}
我使用以下方法解决了这个问题:
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
dismiss(animated: false) {
self.uploadProfileImage(image: image)
}
}
else {
dismiss(animated: false) {
}
}
}