Search code examples
iosswiftuiphlivephoto

How to display PHLivePhotoView in SwiftUI


I'm working on a basic app for live photos and try to build user interface on SwiftUI. However, I don't understand how to display Live Photos properly. Is it possible to incorporate PHLivePhotoView into SwiftUI?


Solution

  • As @Asperi mentioned, UIViewRepresentable wrapper is a way to go:

    import SwiftUI
    import PhotosUI
    
    struct LivePhotoView: UIViewRepresentable {
        @Binding var livephoto: PHLivePhoto
    
        func makeUIView(context: Context) -> PHLivePhotoView {
            return PHLivePhotoView()
        }
    
        func updateUIView(_ lpView: PHLivePhotoView, context: Context) {
            lpView.livePhoto = livephoto
        }
    }