Access the Camera and Photo Library — iOS SwiftUI

Jakir Hossain
2 min readNov 27, 2023

Load an image:

It’s easy to select an image from the gallery. We can use PhotosPicker for that. To use PhotosPicker, we need to import PhotosUI.

Here is the complete example of how to select an image in SwiftUI using PhotosPicker:

import SwiftUI
import PhotosUI

struct ContentView: View {

@State private var selectedItem: PhotosPickerItem?
@State var image: UIImage?

var body: some View {

VStack {
PhotosPicker("Select an image", selection: $selectedItem, matching: .images)
.onChange(of: selectedItem) {
Task {
if let data = try? await selectedItem?.loadTransferable(type: Data.self) {
image = UIImage(data: data)
}
print("Failed to load the image")
}
}

if let image {
Image(uiImage: image)
.resizable()
.scaledToFit()
}
}
.padding()
}
}

#Preview {
ContentView()
}

When we select an image, it returns us image data. We have to convert it to UIImage. After receiving the image, we are loading it into an image view.

Capture an image using the camera:

If we only need to select an image, we can do it using PhotosPicker. PhotosPicker doesn’t allow us to capture photos using the camera. To capture an image using the camera, we have to use ImagePicker.

Here is an example of how to access the camera in swiftUI using ImagePicker:

import SwiftUI
import PhotosUI

struct ContentView: View {
@State private var showCamera = false
@State private var selectedImage: UIImage?
@State var image: UIImage?
var body: some View {
VStack {
if let selectedImage{
Image(uiImage: selectedImage)
.resizable()
.scaledToFit()
}

Button("Open camera") {
self.showCamera.toggle()
}
.fullScreenCover(isPresented: self.$showCamera) {
accessCameraView(selectedImage: self.$selectedImage)
}
}
}
}


struct accessCameraView: UIViewControllerRepresentable {

@Binding var selectedImage: UIImage?
@Environment(\.presentationMode) var isPresented

func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .camera
imagePicker.allowsEditing = true
imagePicker.delegate = context.coordinator
return imagePicker
}

func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {

}

func makeCoordinator() -> Coordinator {
return Coordinator(picker: self)
}
}

// Coordinator will help to preview the selected image in the View.
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var picker: accessCameraView

init(picker: accessCameraView) {
self.picker = picker
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else { return }
self.picker.selectedImage = selectedImage
self.picker.isPresented.wrappedValue.dismiss()
}
}

#Preview {
ContentView()
}

To access the camera, we need to add Privacy — Camera Usage Description in Info.plist:

If you run the app, you will able to access camera and capture an image. For simplicity, I have added all the code in one file (ContentView.swift). You better separate these codes in different files.

--

--