【问题标题】:How to allow user to take picture and share it?如何让用户拍照并分享?
【发布时间】:2019-09-24 11:24:57
【问题描述】:

我正在使用 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 的代码时,共享对话框会显示预填充的文本。

【问题讨论】:

    标签: swift uitableview uiimagepickercontroller uicontextualaction


    【解决方案1】:

    这是一个惨痛的教训。我很高兴解决了我的问题。这是有效的代码。只有一个问题。我现在可以分享从相机拍摄的文字和图片,但我无法使用 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)
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢,我想我对如何实现它有点困惑。可以举个例子吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多