Specifying documents directory as file output URL while working with Glimpse in Swift

为君一笑 提交于 2019-12-11 17:19:33

问题


I'm using Glimpse to record the contents of a UIView so it can save it to a file in the documents directory. I'm having trouble specifying a URL for the file output in the g.startRecording line in viewDidLoad. I'd like to have the return value from getDocumentsDirectory() set as the URL but placing the method name as the parameter does not work. I have also tried to use documentsDirectory and this does not work either and results in "Ambiguous use of 'startRecording" I am using Swift here is my code:

import UIKit

class ViewController: UIViewController, VLCMediaPlayerDelegate {

    @IBOutlet weak var videoView: UIView!

    var player: VLCMediaPlayer = VLCMediaPlayer()
    let g: Glimpse = Glimpse()

    let url = URL(string: "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let media = VLCMedia(url: url!)
        player.media = media
        player.delegate = self
        player.drawable = videoView


        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectory = paths[0]


        player.play()
        g.startRecording(videoView) { (documentsDirectory) in
            <#code#>
        }

    }

    func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectory = paths[0]
        return documentsDirectory
    }

    @IBAction func stopButtonPressed(_ sender: UIButton) {
        player.stop()
        recorder.stop()
    }
}

回答1:


You are using urls[0] when you should just use .url instead. Example:

func write(_ data: Data, to filename: String) throws {
    let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
    let fileURL = documentDirectory.appendingPathComponent(filename)
    try data.write(to: fileURL)
}


来源:https://stackoverflow.com/questions/55570539/specifying-documents-directory-as-file-output-url-while-working-with-glimpse-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!