鑒于以下情況StreamView():
struct StreamView: View {
@StateObject var stream = MJPEGStream()
var body: some View {
MpegView(mjpegStream: self.stream)
.background(.red)
.frame(width: 200, height: 200)
}
}
struct StreamView_Previews: PreviewProvider {
static var previews: some View {
StreamView()
}
}
我有以下MpegView()實作ObservableObject:
class MJPEGStream: ObservableObject {
@Published var stream = MJPEGStreamLib()
init() {
self.stream.play(url: URL(string: "http://192.168.1.120/mjpeg/1")!)
}
}
struct MpegView: View {
@ObservedObject var mjpegStream: MJPEGStream
var body: some View {
Image(uiImage: self.mjpegStream.stream.image)
.resizable()
}
}
基本上,以下類用var image = UIImage()MJPEG 流的更新影像替換了一個實體:
class MJPEGStreamLib: NSObject, URLSessionDataDelegate {
enum StreamStatus {
case stop
case loading
case play
}
var receivedData: NSMutableData?
var dataTask: URLSessionDataTask?
var session: Foundation.URLSession!
var status: StreamStatus = .stop
var authenticationHandler: ((URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))?
var didStartLoading: (() -> Void)?
var didFinishLoading: (() -> Void)?
var contentURL: URL?
var image = UIImage()
override init() {
super.init()
self.session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
}
convenience init(contentURL: URL) {
self.init()
self.contentURL = contentURL
self.play()
}
deinit {
self.dataTask?.cancel()
}
// Play function with url parameter
func play(url: URL) {
// Checking the status for it is already playing or not
if self.status == .play || self.status == .loading {
self.stop()
}
self.contentURL = url
self.play()
}
// Play function without URL paremeter
func play() {
guard let url = self.contentURL, self.status == .stop else {
return
}
self.status = .loading
DispatchQueue.main.async {
self.didStartLoading?()
}
self.receivedData = NSMutableData()
let request = URLRequest(url: url)
self.dataTask = self.session.dataTask(with: request)
self.dataTask?.resume()
}
// Stop the stream function
func stop() {
self.status = .stop
self.dataTask?.cancel()
}
// NSURLSessionDataDelegate
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
// Controlling the imageData is not nil
if let imageData = self.receivedData, imageData.length > 0,
let receivedImage = UIImage(data: imageData as Data) {
if self.status == .loading {
self.status = .play
DispatchQueue.main.async {
self.didFinishLoading?()
}
}
// Set the imageview as received stream
DispatchQueue.main.async {
self.image = receivedImage
}
}
self.receivedData = NSMutableData()
completionHandler(.allow)
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
self.receivedData?.append(data)
}
// NSURLSessionTaskDelegate
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
var credential: URLCredential?
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
// Getting the authentication if stream asks it
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
if let trust = challenge.protectionSpace.serverTrust {
credential = URLCredential(trust: trust)
disposition = .useCredential
}
} else if let onAuthentication = self.authenticationHandler {
(disposition, credential) = onAuthentication(challenge)
}
completionHandler(disposition, credential)
}
}
然后在我的主要ContentView()我只有:
struct ContentView: View {
var body: some View {
StreamView()
}
}
問題是Image中的MpegView()沒有使用從流中接收到的幀進行更新。我不確定這是否是我對類別庫@Published或@StateObject屬性的實作。
注意:我可以通過網路瀏覽器確認流是否有效,并且如果我除錯receivedImage它是來自流視頻的實際幀是什么。
uj5u.com熱心網友回復:
你觀察到的屬性的值stream中MJPEGStream是一個指向一個MJPEGStreamLib物件。
屬性更改的唯一時間,也是您ObservableObject將導致MpegView更新的唯一時間,是您第一次為指標賦值時 -MpegView首次創建時。之后,指向物件的指標永遠不會改變,即使它指向的物件正在快速生成影像。所以你的視圖永遠不會更新。
如果您希望您的 Swift 視圖在MJPEGStreamLib物件中的影像發生更改時更新,那么您需要MJPEGStreamLib將ObservableObject和 標記其image屬性為@Published。
uj5u.com熱心網友回復:
MJPEGStreamLib 包含靜態影像變數,因此影像視圖不會更新。您需要使用諸如@State 或@Published 之類的屬性包裝器來系結變數。仍然無法正常作業,請在此處發表評論。我會幫你
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/327994.html
