我如何允许用户从他们的苹果设备中选择图像?
How can I allow a user to pick an image from their apple device?
我是 swift 应用程序开发的新手,所以我正在学习苹果的食物追踪器教程。我试图让用户将图像设置为他们设备上的图像。我完全复制了他们的代码,但它给我一个错误
Cannot subscript a value of type '[String : AnyObject]' with an index of type 'UIImagePickerController.InfoKey'.
这是我的代码,如果您发现问题请告诉我。 p.s。抱歉代码格式,我不知道如何在堆栈上更改它。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject ]){
// 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)
}
在 Swift 4.2 中,delegate method 已更改为:
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
并且 UIImagePickerControllerOriginalImage
已重命名为 UIImagePickerController.InfoKey.originalImage
。
您应该更改您的方法签名和您使用的密钥名称。您的代码应该可以正常工作。
我是 swift 应用程序开发的新手,所以我正在学习苹果的食物追踪器教程。我试图让用户将图像设置为他们设备上的图像。我完全复制了他们的代码,但它给我一个错误
Cannot subscript a value of type '[String : AnyObject]' with an index of type 'UIImagePickerController.InfoKey'.
这是我的代码,如果您发现问题请告诉我。 p.s。抱歉代码格式,我不知道如何在堆栈上更改它。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject ]){
// 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)
}
在 Swift 4.2 中,delegate method 已更改为:
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
并且 UIImagePickerControllerOriginalImage
已重命名为 UIImagePickerController.InfoKey.originalImage
。
您应该更改您的方法签名和您使用的密钥名称。您的代码应该可以正常工作。