在tvOS Swift中实时流式传输JPEG图像

问题描述

我们正在使用ZoneMinder实时凸轮流媒体软件,该软件在其中流传输JPEG图像(MJPEG),如果应用于HTML <img/>标签,该标签每30秒显示一次自动更新的图片,则效果很好:

<html>
    <body>
        <img src="https://oursite/cgi-bin-zm/nph-zms?scale=50&width=1920px&height=1080px&mode=jpeg&maxfps=30&monitor=21&connkey=150000&rand=1598607232" width="860px" height="340px"/>
    </body>
</html>

使用Swift 5,我尝试通过以下方式将URL /图像打印到UIImageView

let liveURL = URL(string:"https://oursite/cgi-bin-zm/nph-zms?scale=50&width=1920px&height=1080px&mode=jpeg&maxfps=30&monitor=21&connkey=150000&rand=1598607232")
let session = URLSession(configuration: URLSessionConfiguration.default,delegate: self,delegateQueue:OperationQueue.main)
        
session.dataTask(with: url) { (data,request,error) in
     guard let data = data,error == nil else { return }
     print("Download Finished")
     dispatchQueue.main.async() { [weak self] in
         self?.imgLive.image = UIImage(data: data)
     }
}.resume()

最初,我在访问域时遇到自签名SSL错误,使用self作为URLSessionDelegate后,此问题得以解决

我通过上述代码块确认可以从其他远程位置获取任何常规图像。

使用实时图像提供者URL,但是我看到data方法下的dataTask始终为0字节。

如何访问此类URL可以提供流图像,并在Swift中有效显示

解决方法

我认为当有人已经发明轮子时,我不需要重新发明轮子。

https://github.com/WrathChaos/MJPEGStreamLib-这对我来说很有效,可以从远程位置流式传输MJPEG图像!