In Swift, videos appear if I take a video, but not appearing if I have just taken an image

24 Views Asked by At

I am building an app like BeReal that uses the Camera and takes photos/videos, but where you aren't supposed to see your pictures/images until the morning after you take them. I am using CoreData to store the paths of the images/videos, and everything was working fine until I started experimenting with photo/video. If I open the app and take a video, it appears. If I take another video, both of them are in a TabView and appear. If I take a photo, that photo appears only and the videos have a playback button with a cross in it. If I take another video, all videos and photos are visible.

let dirPathNoScheme = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String

           //add directory path file Scheme;  some operations fail w/out it
           let dirPath = "file://\(dirPathNoScheme)"
           //name your file, make sure you get the ext right .mp3/.wav/.m4a/.mov/.whatever
           let fileName = UUID().uuidString + ".mov"
           let pathArray = [dirPath, fileName]
           let path = URL(string: pathArray.joined(separator: "/"))

           //use a guard since the result is an optional
           guard let filePath = path else {
               //if it fails do this stuff:
               return URL(string: "failure")!
           }
           //if it works return the filePath
           return filePath
       }
        
    
        let destinationURL = createNewDirPath()
        
        DispatchQueue.main.async {
            print(outputFileURL)
            self.delegate?.didFinishVideoRecording(destinationURL)
            do {
                let videoData = try Data(contentsOf: outputFileURL)
                try videoData.write(to: destinationURL)
            } catch {
                print("Error saving movie file to documents directory: \(error.localizedDescription)")
            }
        }

This is how I am creating the video. Like I said, it does load on certain occasions, so it's not like I am setting up or loading the wrong file path.

if imagePath.contains(".jpeg") {
                        
                        if let image = loadImage(from: imagePath) {
                            Image(uiImage: image).resizable()
                                .scaledToFill()
                                .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
                                .blur(radius: isBlurred ? 5 : 0) // Apply blur effect conditionally
                        }
                    }
                    else if imagePath.contains(".mov"){
                        
                        VideoPlayerView(data: Video(id: index, player: AVPlayer(url: URL(fileURLWithPath: imagePath)), replay: true), index: index, listIndex: listIndex).frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
                    }

This is how I load photo/video.

Optional("file:///var/mobile/Containers/Data/Application/C59192CC-8611-47C6-96DD-556DB1F35F4B/Documents/13.jpeg") Optional("file:///var/mobile/Containers/Data/Application/C59192CC-8611-47C6-96DD-556DB1F35F4B/Documents/F49690BA-8BD5-4316-B73C-F8FD76A32CC9.mov") Optional("file:///var/mobile/Containers/Data/Application/C59192CC-8611-47C6-96DD-556DB1F35F4B/Documents/15.jpeg")

This is the output when I print the image/video URL, so they are in the same folder

Any ideas of what the issue is when I take a photo? I am using SwiftUICam (https://github.com/pierreveron/SwiftUICam) if that helps

0

There are 0 best solutions below