dro*_*ati 46 uiimagepickercontroller ios swift
我正在使用Apple的Swift iOS教程.这是一个错误,
无法下标类型'[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)
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Xcode Version 10.0 beta 3,其中包括Swift 4.2.
我想了解如何遍历文档以了解可能已发生变化或破坏的内容.
vad*_*ian 102
Swift 4.2中该方法的签名已更改
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
Run Code Online (Sandbox Code Playgroud)
你必须写
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
Run Code Online (Sandbox Code Playgroud)
您可以通过阅读文档或注释掉整个方法来自己找出这些术语更改,重新键入前几个字符并使用代码完成.
edy*_*chk 23
我也遵循相同的教程,更新的代码如下所示:
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)
}
Run Code Online (Sandbox Code Playgroud)
这样使用
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
Run Code Online (Sandbox Code Playgroud)
在最新版本的swift 4或5中,下面给出了委托方法,它应该可以工作
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// Code here
}
Run Code Online (Sandbox Code Playgroud)
并使用
guard let selectedImage = info[.editedImage] as? UIImage else {
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.editedImage] as? UIImage else {
}
}
Run Code Online (Sandbox Code Playgroud)
在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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28343 次 |
| 最近记录: |