我正在使用Apple's Swift iOS Tutorial。这是一个错误,
无法下标类型'[String:Any]'的值,其索引类型为'UIImagePickerController.InfoKey'
他们定义的功能如下。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
我正在使用Xcode Version 10.0 beta 3,其中包括Swift 4.2。
我想了解如何遍历文档以了解可能已发生变化或破坏的内容。
Swift 4.2中该方法的签名已更改
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
你必须写
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
您可以通过阅读documentation或通过注释掉整个方法来自己弄清楚这些术语的变化,重新键入前几个字符并使用代码完成。
我也遵循相同的教程,更新的代码如下所示:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
像这样使用,
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
在Swift 4和5中如下:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.editedImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
self.myImageView.image = selectedImage
picker.dismiss(animated: true, completion: nil)
}
在最新版本的swift 4或5中,委托方法如下,它应该可以工作
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// Code here
}
并使用,
guard let selectedImage = info[.editedImage] as? UIImage else {
}
方法在Swift 4.2中有所改变。
首先初始化这两个变量:
var imagePicker = UIImagePickerController()
var pickedImageProduct = UIImage()
extension yourViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
//create an IBAction to access Camera
@IBAction func accessCameraBtn(_ sender: UIButton) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
self.present(imagePicker, animated: true, completion: nil)
}
//create an IBAction to access Gallery
@IBAction func accessGalleryBtn(_ sender: UIButton) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
//Final step put this Delegate
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else {
Print("Error: \(info)")
}
pickedImageProduct = selectedImage
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
在Swift 4.2中,您可以将函数编写为:
func photoLib()
{
//opens Photo Library, call the function in a @IBAction func
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType =
UIImagePickerController.SourceType.photoLibrary
myPickerController.allowsEditing = true
present(myPickerController, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true)
guard let image = info[.editedImage] as? UIImage else {
print("No image found")
photoLabel.text = "Photo Not Found"
return
}
}