Search code examples
swiftcamerasnapshotcoremlhomekit

HMCameraSnapshot to UIImage


I'm playing the a homekit camera device where I can get the stream video an display it in a HMCameraView. I need to take snapshot of this video stream and this can be done using cameraSnapshotControl function. Works great I can see using the delegate function : HMCameraSnapshotControlDelegate that snapshot are taken. However I spent hours looking how to convert this snapshot in a basic image like UIImage... I do not need to display this image but analyse it (CoreML). Someone already try this? Seems that nobody code with Homekit Apple framework...


Solution

  • I just ran into this same issue and I couldn't find anything in Apple's official documentation explaining how to do it. After playing around with a few things, I eventually discovered that you can do this by setting the cameraSource property on HMCameraView, which accepts both video streams and snapshots.

    @IBOutlet weak var cameraLiveStreamView: HMCameraView!
    ...
    
    func cameraSnapshotControl(_ cameraSnapshotControl: HMCameraSnapshotControl, didTake snapshot: HMCameraSnapshot?, error: Error?) {
       cameraLiveStreamView?.cameraSource = cameraSnapshotControl.mostRecentSnapshot
    }
    

    Hope this helps!