0

我正在使用 Xcode 10,swift 5。我有一个 tableViewController,允许用户点击、左右滑动。当向右滑动时,它会显示一个显示完成的前导按钮。当用户点击按钮时,会出现一个共享对话框并允许他们共享工作。我正在努力实现一个 UIPickerController,它允许用户为他们的项目拍照并将其嵌入到共享对话框中。

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let complete = UIContextualAction.init(style: .normal, title: "Complete") { (action, view, completion) in
        let completedTxt = "Look at what I fixed, thanks to @DIY Home Repair!"

        let vc = UIImagePickerController()
            vc.sourceType = .camera
            vc.allowsEditing = false
            vc.delegate = self

        self.present(vc, animated: true)

        let activityController = UIActivityViewController(activityItems: [completedTxt, ], applicationActivities: nil)

        self.present(activityController, animated: true, completion: nil)
        completion(true) // Completion

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    picker.dismiss(animated: true)

    guard let image = info[.originalImage] as? UIImage else {
        print("No image found")
        return
    }

    // print out the image size as a test
    print(image.size)
}

}

该代码显示了相机并允许用户拍照,但图片无法到达任何位置,共享对话框也不会出现。当我删除 UIPickerController 的代码时,共享对话框会显示预填充的文本。

4

2 回答 2

0

这是因为当你展示一个控制器时,已经有一个 UI 任务在运行,因此你不能一次展示 2 个控制器。

根据您的问题,您应该使用UIActivityViewControllerdidFinishPickingMediaWithInfo方法,以便您可以将图像添加到共享表中。

于 2019-05-06T20:42:05.873 回答
0

这是一个惨痛的教训。我很高兴解决了我的问题。这是有效的代码。只有一个问题。我现在可以分享从相机拍摄的文字和图片,但我无法使用 facebook 或他们的 Messenger 应用程序分享。猜猜这是另一个问题。

 override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let complete = UIContextualAction.init(style: .normal, title: "Complete") { (action, view, completion) in

                if UIImagePickerController.isSourceTypeAvailable(.camera) {
                    self.picker.allowsEditing = false
                    self.picker.sourceType = UIImagePickerController.SourceType.camera
                    self.picker.cameraCaptureMode = .photo
                    self.picker.modalPresentationStyle = .fullScreen
                    self.present(self.picker,animated: true,completion: nil)
                } else {
                    self.noCamera()
                }

        }

     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    guard let myImageView = info[.originalImage] as? UIImage else {
        return
    }
    print(myImageView)

    if myImageView != nil {
        self.dismiss(animated: true, completion: nil)
        let activityController = UIActivityViewController(activityItems: [self.completedTxt, myImageView as Any], applicationActivities: nil)

        present(activityController, animated: true, completion: nil)

    }else {
        return
    }
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}
于 2019-05-08T04:48:10.757 回答