How to allow user to take picture and share it?

68 Views Asked by At

I am using Xcode 10, swift 5. I have a tableViewController that allows a user to tap, swipe left and right. When the swipe right it reveals a leading button that says complete. When the user taps the button a share dialog appears and allows them to share the work. I am struggling to implement a UIPickerController that allows the user to take a picture of their project and embed it in the share dialog.

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)
}

}

The code displays the camera and allows the user to take a picture but the picture goes no where and the share dialog doesn't appear. When I remove the code for the UIPickerController the share dialog appears with the pre-populated text.

2

There are 2 best solutions below

1
Dinesh Tanwar On

Its because when you present a controller there is already a UI task in motion and hence you cannot present 2 controller at once.

according to your problem you should put UIActivityViewController at the didFinishPickingMediaWithInfo method so that you can add your image in the share sheet.

0
AudioBubble On

This was a hard lesson learned. I am glad to have figured out my issue. Here is the code that works. There is just one issue. I can now share the text and picture taken from the camera but I cannot share it using facebook or their messenger app. Guess thats for another question.

 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)
}