我正在尝试为用户提供两个上传图片的选项,第一个是照片库,第二个是相机。我有一个操作表,用户可以在其中选择他要上传图像的方式,但是当单击“相机”选项时,相机应用将无法打开。我确实在plist文件中添加了所需的隐私。 这是我的动作表代码:
extension MoreReportsVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func handleAddPhoto() {
showSimpleActionSheet(controller: self)
}
func showSimpleActionSheet(controller: UIViewController) {
let alert = UIAlertController(title: "Add a photo", message: "Please Select an option", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "From photo library", style: .default, handler: { (_) in
self.imagePicker.getImage(fromSourceType: .photoLibrary)
}))
alert.addAction(UIAlertAction(title: "Open camera app", style: .default, handler: { (_) in
self.imagePicker.getImage(fromSourceType: .camera)
}))
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { (_) in
print("User click Dismiss button")
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}
}
这是我的ImagePicker代码:
class ImagePickerManager: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var placeholderImage: UIImageView?
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//TODO:
}
func getImage(fromSourceType sourceType: UIImagePickerController.SourceType){
if UIImagePickerController.isSourceTypeAvailable(sourceType){
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = sourceType
self.present(imagePickerController, animated: true, completion: nil)
}
}
}
操作表中的imagePicker只是一个ImagePickerManager变量。 该应用程序不会崩溃或发生任何事情,它可以进入getImage方法,但不要打开相机。
编辑:我认为问题出在委托=自我中。自我需要是VC,如果我没记错的话,我会尝试查看图像选择器。当前是ImagePickerManager,但是我找不到将其设置为VC的方法。
答案 0 :(得分:0)
在getImage
扩展名中添加UIViewController
方法,
extenstion UIViewController {
func getImage(fromSourceType sourceType: UIImagePickerController.SourceType){
if UIImagePickerController.isSourceTypeAvailable(sourceType){
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = sourceType
self.present(imagePickerController, animated: true, completion: nil)
} else {
print("Source type isn't available")
}
}
}
然后从showSimpleActionSheet
的{{1}}方法调用此方法
self
答案 1 :(得分:0)
问题是您的ImagePickerManager
实际上不是对该接口中当前任何视图控制器的引用。因此,self.present
不执行任何操作,因为self
不是当前的视图控制器。
如果要将图像选择器管理放入不是当前视图控制器的封装类中,则封装不应本身成为视图控制器;它应该只是一个普通的对象。这样一来,您将无法说出self.present
,因此您需要为该对象提供对当前视图控制器(在本例中为MoreReportsVC)的引用。