如何允许用户拍照和分享?

时间:2019-05-06 20:08:35

标签: swift uitableview uiimagepickercontroller uicontextualaction

我正在使用Xcode 10,swift5。我有一个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的代码时,共享对话框随即出现,并带有预先填充的文本。

2 个答案:

答案 0 :(得分:0)

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

根据您的问题,应将UIActivityViewController放在didFinishPickingMediaWithInfo方法中,以便可以将图像添加到共享表中。

答案 1 :(得分: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)
}