Yummypets / YPImagePicker

📸 Instagram-like image picker & filters for iOS
MIT License
4.31k stars 974 forks source link

Upload Video to Photos App #714

Open zewkini opened 2 years ago

zewkini commented 2 years ago

Would like to see a clear way to upload/save videos to Apple's Photos app. Right now it only saves to the YPImagePicker's tmp data container on a mobile device and there is no easy way to access them for editing or upload them to the Photos app. You can save photos easily to the Photos app but not videos.

NikKovIos commented 2 years ago

So what exactly you want? The preferable way.

zewkini commented 2 years ago

In VideoFiltersVC.swift, at line 127, I changed the code in the do-catch function to where I commented out the let path:String = destinationURL.path and added two UI functions to check if the video can be saved to the Photos album and then save it if it is ok to save. It's been a while since I worked on it so I'm not entirely sure if that's all I did.

    do {
        let asset = AVURLAsset(url: inputVideo.url)
        let trimmedAsset = try asset
            .assetByTrimming(startTime: trimmerView.startTime ?? CMTime.zero,
                             endTime: trimmerView.endTime ?? inputAsset.duration)

        // Looks like file:///private/var/mobile/Containers/Data/Application
        // /FAD486B4-784D-4397-B00C-AD0EFFB45F52/tmp/8A2B410A-BD34-4E3F-8CB5-A548A946C1F1.mov
        let destinationURL = URL(fileURLWithPath: NSTemporaryDirectory())
                .appendingUniquePathComponent(pathExtension: YPConfig.video.fileType.fileExtension)

            _ = trimmedAsset.export(to: destinationURL) { [weak self] session in

                //let path:String = destinationURL.path
                if
                    UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(destinationURL.path)
                {
                    print ("YES")

                    UISaveVideoAtPathToSavedPhotosAlbum(destinationURL.path, nil, nil, nil)
                }
            switch session.status {
            case .completed:
                DispatchQueue.main.async {
                    if let coverImage = self?.coverImageView.image {
                        let resultVideo = YPMediaVideo(thumbnail: coverImage,
                                videoURL: destinationURL,
                                asset: self?.inputVideo.asset)
                        didSave(YPMediaItem.video(v: resultVideo))
                        self?.setupRightBarButtonItem()
                    } else {
                        ypLog("Don't have coverImage.")
                    }
                }
            case .failed:
                ypLog("Export of the video failed. Reason: \(String(describing: session.error))")
            default:
                ypLog("Export session completed with \(session.status) status. Not handled")
            }
        }
    } catch let error...