How to Save ReplayKit Video to Camera Roll with In-App Button

前端 未结 4 1868
自闭症患者
自闭症患者 2021-02-04 02:50

I am relatively new to iOS development and Swift but I have an app I\'m working on which is supposed to record the activity on the screen and save the resulting video to the cam

4条回答
  •  走了就别回头了
    2021-02-04 03:48

    Yes, you can. Check this ReplayKit2 Swift 4:

    https://medium.com/@giridharvc7/replaykit-screen-recording-8ee9a61dd762

    Once you have the file, it shouldn't be too much trouble to save it to the camera roll with something along the lines of:

    static func saveVideo(url: URL, returnCompletion: @escaping (String?) -> () ) {
    
            DispatchQueue.global(qos: .userInitiated).async { 
    
                guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
    
                if !FileManager.default.fileExists(atPath: documentsDirectoryURL.appendingPathComponent(url.lastPathComponent).path) {
    
                    URLSession.shared.downloadTask(with: url) { (location, response, error) -> Void in
    
                        guard let location = location else { return }
    
                        let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggestedFilename ?? url.lastPathComponent)
    
                        do {
                            try FileManager.default.moveItem(at: location, to: destinationURL)
                            PHPhotoLibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> Void in
                                if authorizationStatus == .authorized {
                                    PHPhotoLibrary.shared().performChanges({
                                        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed, error in
                                            DispatchQueue.main.async {
                                                if completed {                                                    returnCompletion(url.lastPathComponent)
                                                } else {
                                                    returnCompletion(nil)
                                                }
                                            }
                                    }
                                }
                            })
                            returnCompletion(url.lastPathComponent)
                        } catch {
                            returnCompletion(nil)
                        }
    
                    }.resume()
    
                } else {
                    returnCompletion(nil)
                }
            }
    
        }
    

提交回复
热议问题