Generating Live Photo from JPG and MOV fails

I am working on an iOS application using SwiftUI where I want to convert a JPG and a MOV file to a live photo. I am utilizing the LivePhoto Class from Github for this. The JPG and MOV files are displayed correctly in my WallpaperDetailView, but I am facing issues when trying to download the live photo to the gallery and generate the Live Photo.

Here is the relevant code and the errors I am encountering:

Console prints: Play button should be visible Image URL fetched and set: Optional("https://firebasestorage.googleapis.com/...") Video is ready to play Video downloaded to: file:///var/mobile/Containers/Data/Application/.../tmp/CFNetworkDownload_7rW5ny.tmp Failed to generate Live Photo

  • I have verified that the app has the necessary permissions to access the Photo Library.
  • The JPEG and MOV files are successfully downloaded and can be displayed in the app.
  • The issue seems to occur when generating the Live Photo from the downloaded files.
struct WallpaperDetailView: View {
    var wallpaper: Wallpaper
    @State private var isLoading = false
    @State private var isImageSaved = false
    @State private var imageURL: URL?
    @State private var livePhotoVideoURL: URL?
    @State private var player: AVPlayer?
    @State private var playerViewController: AVPlayerViewController?
    @State private var isVideoReady = false
    @State private var showBuffering = false

    var body: some View {
        ZStack {
            if let imageURL = imageURL {
                GeometryReader { geometry in
                    KFImage(imageURL)
                        .resizable()
                        ...
                }
            }
            
            if let playerViewController = playerViewController {
                VideoPlayerViewController(playerViewController: playerViewController)
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .clipped()
                    .edgesIgnoringSafeArea(.all)
            }
            
            
        }
        .onAppear {
            PHPhotoLibrary.requestAuthorization { status in
                if status == .authorized {
                    loadImage()
                } else {
                    print("User denied access to photo library")
                }
            }
         
    }
    
    private func loadImage() {
        isLoading = true
        
        if let imageURLString = wallpaper.imageURL, let imageURL = URL(string: imageURLString) {
            self.imageURL = imageURL
            if imageURL.scheme == "file" {
                self.isLoading = false
                print("Local image URL set: \(imageURL)")
            } else {
                fetchDownloadURL(from: imageURLString) { url in
                    self.imageURL = url
                    self.isLoading = false
                    print("Image URL fetched and set: \(String(describing: url))")
                }
            }
        }
        
        if let livePhotoVideoURLString = wallpaper.livePhotoVideoURL, let livePhotoVideoURL = URL(string: livePhotoVideoURLString) {
            self.livePhotoVideoURL = livePhotoVideoURL
            preloadAndPlayVideo(from: livePhotoVideoURL)
        } else {
            self.isLoading = false
            print("No valid image or video URL")
        }
    }
    
    private func preloadAndPlayVideo(from url: URL) {
        self.player = AVPlayer(url: url)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = self.player
        self.playerViewController = playerViewController
        
        let playerItem = AVPlayerItem(url: url)
        playerItem.preferredForwardBufferDuration = 1.0
        self.player?.replaceCurrentItem(with: playerItem)
        
        ...
      
        print("Live Photo Video URL set: \(url)")
    }
    
    private func saveWallpaperToPhotos() {
        if let imageURL = imageURL, let livePhotoVideoURL = livePhotoVideoURL {
            saveLivePhotoToPhotos(imageURL: imageURL, videoURL: livePhotoVideoURL)
        } else if let imageURL = imageURL {
            saveImageToPhotos(url: imageURL)
        }
    }
    
    private func saveImageToPhotos(url: URL) {
        ...
    }
    
    private func saveLivePhotoToPhotos(imageURL: URL, videoURL: URL) {
        isLoading = true
        downloadVideo(from: videoURL) { localVideoURL in
            guard let localVideoURL = localVideoURL else {
                print("Failed to download video for Live Photo")
                DispatchQueue.main.async {
                    self.isLoading = false
                }
                return
            }
            print("Video downloaded to: \(localVideoURL)")
            self.generateAndSaveLivePhoto(imageURL: imageURL, videoURL: localVideoURL)
        }
    }
    
    private func generateAndSaveLivePhoto(imageURL: URL, videoURL: URL) {
        LivePhoto.generate(from: imageURL, videoURL: videoURL, progress: { percent in
            print("Progress: \(percent)")
        }, completion: { livePhoto, resources in
            guard let resources = resources else {
                print("Failed to generate Live Photo")
                DispatchQueue.main.async {
                    self.isLoading = false
                }
                return
            }
            print("Live Photo generated with resources: \(resources)")
            self.saveLivePhotoToLibrary(resources: resources)
        })
    }
    
    private func saveLivePhotoToLibrary(resources: LivePhoto.LivePhotoResources) {
        LivePhoto.saveToLibrary(resources) { success in
            DispatchQueue.main.async {
                if success {
                    self.isImageSaved = true
                    print("Live Photo saved successfully")
                } else {
                    print("Failed to save Live Photo")
                }
                self.isLoading = false
            }
        }
    }
    
    private func fetchDownloadURL(from gsURL: String, completion: @escaping (URL?) -> Void) {
        let storageRef = Storage.storage().reference(forURL: gsURL)
        storageRef.downloadURL { url, error in
            if let error = error {
                print("Failed to fetch image URL: \(error)")
                completion(nil)
            } else {
                completion(url)
            }
        }
    }
    
    private func downloadVideo(from url: URL, completion: @escaping (URL?) -> Void) {
        let task = URLSession.shared.downloadTask(with: url) { localURL, response, error in
            guard let localURL = localURL, error == nil else {
                print("Failed to download video: \(String(describing: error))")
                completion(nil)
                return
            }
            completion(localURL)
        }
        task.resume()
    }
}```


Generating Live Photo from JPG and MOV fails
 
 
Q