我正在制作一个应用程序,显示流视频(由rtmp
)。我已经开始用MobileVLCKit
了。我需要一个功能,将使视频快照。我已经尝试了内置函数
func saveVideoSnapshotAt(path: String!, withWidth: UInt32, andHeight: UInt32)
但是它不起作用。这是支持在videolan
论坛上写的:
"此方法仅适用于OS X。虽然它在iOS上是暴露的(这是它应该做的)不是,我同意),它不会在移动设备上工作。"
然后我尝试使用标准的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
背景截图,而不是视频。有没有人知道是否有任何解决方案,或者至少我应该继续寻找哪个方向?
解决方案比我想象的要简单。
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();
您是否像这样在代码中为mediaPlayer添加了委托https://github.com/maknapp/vlckitSwiftSample/blob/master/vlckitSwiftSample/ViewController.swift