Can we add Photo Library support in VisionKit?

31 Views Asked by At

Is it possible to integrate photo library support into VisionKit? Upon inspection, it seems that VisionKit lacks options for selecting the source type, and there isn't a designated function for passing images directly.

There was another library named vision will it help ? if yes then how ?

Are there alternative methods available for scanning images from the photo library in SwiftUI?

1

There are 1 best solutions below

1
Hamza On

While Vision itself doesn't directly offer support for accessing the photo library, you can use other frameworks such as UIKit or SwiftUI to implement image picker functionality and then pass the selected image to Vision for analysis.

Here's a general approach on how you can integrate Vision with UIKit for image selection from the photo library:

import UIKit
import Vision

class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Call method to present image picker
        presentImagePicker()
    }

    func presentImagePicker() {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        present(imagePicker, animated: true, completion: nil)
    }

    // MARK: - UIImagePickerControllerDelegate

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        dismiss(animated: true, completion: nil)
        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            print("Failed to retrieve image from picker.")
            return
        }

        // Pass the selected image to Vision for analysis
        analyzeImage(image)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

    func analyzeImage(_ image: UIImage) {
        // Perform image analysis using Vision framework
        // You can perform tasks such as object detection, text recognition, etc.
    }
}