0

I have a project that requires me to take a picture and send it to the provided API. I don't want to save it locally, the picture of the image should be sent to the API immediately once the picture is captured.

I've checked the Stack Overflow question swift Take a photo and save to photo library, but it seems like the image is stored locally.

Here's my code:

func cameraSetup(){

    let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

    do {
        let input = try AVCaptureDeviceInput(device: captureDevice!)

        captureSession = AVCaptureSession()
        captureSession?.addInput(input)

        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoPreviewLayer?.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height - 150)
        self.cameraView.layer.addSublayer(videoPreviewLayer!)

        captureSession?.startRunning()
    } catch {
        print(error)
    }
  }
}

How can I send it off immediately instead of having to save it locally first?

grooveplex
  • 2,492
  • 4
  • 28
  • 30
R.Karim
  • 85
  • 1
  • 6
  • You done it so wrong my friend. You should consider using `UIImagePickerController` for these kind of tasks. – Mojtaba Hosseini Feb 11 '19 at 17:13
  • @MojtabaHosseini I have just tested it, and I have until the capture. And now I am quite stuck on getting the data of the image. Thanks for guidance. I am trying to implement the `UIImagePickerControllerDelegate` to get the `UIImage` – R.Karim Feb 11 '19 at 17:29
  • 2
    Re-read the accepted answer in the link that you provided, then you can see how you can get the image from that delegate. Then you just need to convert the UIImage to pngData or jpegData (see: docs https://developer.apple.com/documentation/uikit/uiimage/1624115-jpegdata) – MartinM Feb 11 '19 at 17:36
  • @MartinM Alright. thank you – R.Karim Feb 11 '19 at 17:42

1 Answers1

0

For task like this, you should consider using UIImagePickerController and its Delegate methods. Best practice for this is to show user options to choose between camera and gallery (Photo library).

class MyViewController: UIViewController {
    func presentImagePickerActionSheet() {
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

        func addCameraAction() {
            guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return assertionFailure("No camera") }
            let pickerAction = UIAlertAction(title: "Camera", style: .default) { _ in
                self.pickImage(source: .camera)
            }
            actionSheet.addAction(pickerAction)
        }

        func addGalleryPickerAction() {
            guard UIImagePickerController.isSourceTypeAvailable(.photoLibrary) else { return assertionFailure("No gallery") }
            let pickerAction = UIAlertAction(title: "Gallery", style: .default) { _ in
                self.pickImage(source: .photoLibrary)
            }
            actionSheet.addAction(pickerAction)
        }

        func addRemoveActionIfNeeded() {
            return() // Do your logic if needed
            let pickerAction = UIAlertAction(title: "Delete", style: .destructive) { _ in

            }
            actionSheet.addAction(pickerAction)
        }

        func addCancelAction() {
            let cancelAction = UIAlertAction(title: "cancel", style: .cancel, handler: nil)
            actionSheet.addAction(cancelAction)
        }

        addCameraAction()
        addGalleryPickerAction()
        addRemoveActionIfNeeded()
        addCancelAction()
        present(actionSheet, animated: true)
    }

    private func pickImage(source: UIImagePickerControllerSourceType) {
        guard UIImagePickerController.isSourceTypeAvailable(source) else { return assertionFailure("Source not found") }
        let imagePickerController = UIImagePickerController()
        imagePickerController.sourceType = source
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = true
        present(imagePickerController, animated: true)
    }
}

extension MyViewController: UIImagePickerControllerDelegate {
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        defer { picker.dismiss(animated: true) }
        let editedImage = info[UIImagePickerControllerEditedImage]
        let originalImage = info[UIImagePickerControllerOriginalImage]
        guard let image = (editedImage ?? originalImage) as? UIImage else { return assertionFailure("Image not found")}

        // Do anything you want with image here




        // In case of need to convert it to data:
        let quality = 1.0
        guard let imageData = UIImageJPEGRepresentation(image, quality) else { return assertionFailure("No image data") }

        // Do anything you want with image data here

    }
}
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278