制作 VLCMediaPlayer (MobileVLCKit) 的快照

Make snapshot of VLCMediaPlayer (MobileVLCKit)

我正在开发一个显示流视频的应用程序(来自 rtmp)。我已经开始使用 MobileVLCKit 了。我需要一个可以制作视频快照的功能。我试过内置函数

    func saveVideoSnapshotAt(path: String!, withWidth: UInt32, andHeight: UInt32)

但是没用。这就是支持在 videolan 论坛上写的关于它的内容:

"This method is OS X only. While it is exposed on iOS (what it should not be, I agree), it won't work on mobile devices."

然后我尝试使用标准 iOS 功能:

    @IBOutlet weak var videoView: UIView!
    var mediaPlayer = VLCMediaPlayer()
    if let url = NSURL(string: "rtmp://someurl") {
        let media = VLCMedia(URL: url)
        mediaPlayer.media = media
        mediaPlayer.drawable = videoView
        mediaPlayer.play()
    }
    UIGraphicsBeginImageContext(videoView.frame.size);
    guard let context = UIGraphicsGetCurrentContext() else {
        print("Couldn't get context")
        return
    }
    videoView.layer.renderInContext(context)
    guard let screenshot = UIGraphicsGetImageFromCurrentImageContext() else {
        UIGraphicsEndImageContext()
        return
    }
    UIGraphicsEndImageContext()

结果我只得到 UIView 背景截图,里面没有视频。有谁知道是否有任何解决方案或至少我应该继续搜索的方向?

您是否像这样在代码中为 mediaPlayer 添加委托 https://github.com/maknapp/vlckitSwiftSample/blob/master/vlckitSwiftSample/ViewController.swift

解决方案比我想象的要简单。

    let size = mediaPlayer.drawable.frame.size

    UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale)

    let rec = mediaPlayer.drawable.frame
    mediaPlayer.drawable.drawViewHierarchyInRect(rec, afterScreenUpdates: false)

    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();