在 tvOS 中直播 JPEG 图像 Swift
Live streaming JPEG images in tvOS Swift
我们正在使用 ZoneMinder 实时摄像头流媒体软件,它可以流式传输 JPEG 图像 (MJPEG),如果应用于每 30 秒显示自动更新图像的 HTML <img/>
标签,则效果很好:
<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/image 打印到 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 as URLSessionDelegate
后解决了这个问题。
我用上面的代码块确认我可以从其他远程位置获取任何常规图像。
使用我们的实时图像提供程序 URL,但是我看到 dataTask
方法下的 data
总是 0 字节。
如何访问这样的 URL 提供的流式图像,并在 Swift 中有效显示?
我想我不需要 re-invent 已经有人发明了轮子。
https://github.com/WrathChaos/MJPEGStreamLib - 这对我从远程位置流式传输 MJPEG 图像非常有效!
我们正在使用 ZoneMinder 实时摄像头流媒体软件,它可以流式传输 JPEG 图像 (MJPEG),如果应用于每 30 秒显示自动更新图像的 HTML <img/>
标签,则效果很好:
<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/image 打印到 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 as URLSessionDelegate
后解决了这个问题。
我用上面的代码块确认我可以从其他远程位置获取任何常规图像。
使用我们的实时图像提供程序 URL,但是我看到 dataTask
方法下的 data
总是 0 字节。
如何访问这样的 URL 提供的流式图像,并在 Swift 中有效显示?
我想我不需要 re-invent 已经有人发明了轮子。
https://github.com/WrathChaos/MJPEGStreamLib - 这对我从远程位置流式传输 MJPEG 图像非常有效!