【发布时间】:2021-01-27 06:34:48
【问题描述】:
我正在尝试向我的 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)
}
到目前为止一切正常,但是当我选择图像时,不会调用委托方法。
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 新手,所以我可能会遗漏一些东西。谁能帮我解决这个问题?
谢谢。
【问题讨论】:
标签: ios swift xcode swift3 uiimagepickercontroller