How to attach images from camera or gallery in Swift

Question:

Good morning everyone, I'm developing an application in Swift, but I'm new to the language and I don't know some things, I'd like to know how I Attach the images from my gallery or open the camera on my phone to take a picture.

Thanks in advance.

Answer:

You can make a giant code by hand or use a pod, which is what I recommend. I've already used the ALCameraViewController pod, which allows both taking photos and choosing from the gallery. You can find his github here:

https://github.com/AlexLittlejohn/ALCameraViewController

Steps for use:

1) Add to your podcast:

pod 'ALCameraViewController'

2) Import in your file that will call the view controller of the camera or pod, with the following line:

import ALCameraViewController

3) When calling the camera's view controller, use the following code:

let cameraViewController = CameraViewController { [weak self] image, asset in
    // A variável image que você recebe aqui vai ter a imagem que o usuário escolheu. Fique atento para o caso que ele cancela a escolha da imagem.
    self?.dismiss(animated: true, completion: nil)
}

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

For more swif, iOS development and entrepreneurship tips subscribe to my youtube channel: https://www.youtube.com/channel/UCNghEgS-PkKMDbZfiwhvYpA

Scroll to Top