我想从库中导入 ios 中的图像,但出现错误
value of type 'viewcontroller' has no member 'present'
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func Alert(sender: AnyObject) {
let nextController = UIImagePickerController()
self.present(nextController, animated: true, completion: nil)
}}
最佳答案
这是我的剥离工作代码。我使用两个选择器 - 一个用于相机,一个用于库 - 因为一些奇怪的 iOS 色调错误。为了避免“大量 View Controller ”问题,我倾向于将与 UIImagePickerController
相关的所有可能内容移动到单独的文件和扩展名中。
class OpenViewController: UIViewController {
let pickerLibrary = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
pickerLibrary.delegate = self
}
}
extension OpenViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@objc func showImagePicker() {
pickerLibrary.allowsEditing = false
pickerLibrary.sourceType = .photoLibrary
present(pickerLibrary,
animated: true,
completion: nil)
pickerLibrary.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
}
虽然present
代码看起来正确,但我注意到您没有在代码中发布一些内容。也许是因为你没有发布它,但我肯定会检查这些内容:
- 您是否将
ViewController
设置为委托(delegate)? - 您是否将
ViewController.view
设置为源 View ? iPhone 可能不需要,但 iPad 肯定有帮助。 - 您是否包含了
UIImagePickerControllerDelegate、UINavigationControllerDelegate
所需的所有内容?
现在,正如您的错误所暗示的那样,可能是其他问题 - UIViewController
可以 呈现
另一个 View Controller 。在这种情况下,您没有给我们提供重复该错误的方法。换句话说,我假设 (1) 您看到一个附加到 Alert()
的 UIButton
- 顺便说一句,Swift 约定使用小写字母 - 并且 (2 )您已经证明,当点击该按钮时,实际上会执行“Alert()”。
关于ios - UIImagePickerController导入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51575827/