Skip to content

Instantly share code, notes, and snippets.

@Epillepsy
Forked from jeffmcfadden/IPCameraView.swift
Last active October 9, 2019 06:25
Show Gist options
  • Save Epillepsy/ba38c808d64369ef9dc65ee0399e6e1a to your computer and use it in GitHub Desktop.
Save Epillepsy/ba38c808d64369ef9dc65ee0399e6e1a to your computer and use it in GitHub Desktop.
A UIView Subclass For Displaying An IPCamera Stream (MJPEG) for swift 3
import Foundation
import UIKit
//MARK: For Swift 3
class IPCameraView: UIView, URLSessionDataDelegate {
var imageView:UIImageView
var url: URL?
var endMarkerData: NSData
var receivedData: NSMutableData
var dataTask: URLSessionDataTask
override init(frame: CGRect) {
self.endMarkerData = NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2)
self.receivedData = NSMutableData()
self.imageView = UIImageView()
self.dataTask = URLSessionDataTask()
super.init(frame: frame)
self.addSubview(self.imageView)
}
required init?(coder aDecoder: NSCoder) {
self.endMarkerData = NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2)
self.receivedData = NSMutableData()
self.imageView = UIImageView()
self.dataTask = URLSessionDataTask()
super.init(coder: aDecoder)
self.addSubview(self.imageView)
}
deinit{
self.dataTask.cancel()
}
func startWithURL(url:URL){
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
let request = URLRequest(url: url as URL )
self.dataTask = session.dataTask(with: request as URLRequest)
// Initialization code
self.dataTask.resume()
let bounds = self.bounds
self.imageView.frame = bounds
self.imageView.contentMode = UIViewContentMode.scaleAspectFit
}
override func layoutSubviews() {
super.layoutSubviews()
}
func pause() {
self.dataTask.cancel()
}
func stop(){
self.pause()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
self.receivedData.append(data as Data)
//NSLog( "Did receive data" )
let endRange:NSRange = self.receivedData.range(of: self.endMarkerData as Data, options: NSData.SearchOptions(), in: NSMakeRange(0, self.receivedData.length))
// NSRange endRange = [_receivedData rangeOfData:_endMarkerData
// options:0
// range:NSMakeRange(0, _receivedData.length)];
let endLocation = endRange.location + endRange.length
//long long endLocation = endRange.location + endRange.length;
if self.receivedData.length >= endLocation {
let imageData = self.receivedData.subdata(with: NSMakeRange(0, endLocation))
let receivedImage = UIImage(data: imageData)
DispatchQueue.main.async( execute: {
self.imageView.image = receivedImage
})
//NSLog( "Length: %d", imageData.length )
self.receivedData = NSMutableData(data: self.receivedData.subdata(with: NSMakeRange(endLocation, self.receivedData.length - endLocation)))
}
// if (_receivedData.length >= endLocation) {
// NSData *imageData = [_receivedData subdataWithRange:NSMakeRange(0, endLocation)];
// UIImage *receivedImage = [UIImage imageWithData:imageData];
// if (receivedImage) {
// self.image = receivedImage;
// }
// }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect)
{
// Drawing code
}
*/
}
@charlsagente
Copy link

Hello, I'm trying to connect tvOS with my raspberry pi stream with motion. But I'm super new using Swift, do you have an example how to use this class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment