Skip to content

Instantly share code, notes, and snippets.

@jeffmcfadden
Created July 2, 2014 22:48
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jeffmcfadden/4970e952466d50eaf533 to your computer and use it in GitHub Desktop.
Save jeffmcfadden/4970e952466d50eaf533 to your computer and use it in GitHub Desktop.
A UIView Subclass For Displaying An IPCamera Stream (MJPEG)
class IPCameraView: UIView, NSURLSessionDataDelegate {
var imageView:UIImageView
var url: NSURL
var endMarkerData: NSData
var receivedData: NSMutableData
var dataTask: NSURLSessionDataTask
init(frame: CGRect) {
self.endMarkerData = NSData(bytes: [0xFF, 0xD9] as Byte[], length: 2)
self.url = NSURL()
self.receivedData = NSMutableData()
self.imageView = UIImageView()
self.dataTask = NSURLSessionDataTask()
super.init(frame: frame)
self.addSubview(self.imageView)
}
deinit{
self.dataTask.cancel()
}
func startWithURL(url:NSURL){
var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: nil)
var request = NSURLRequest(URL: url )
self.dataTask = session.dataTaskWithRequest(request)
// Initialization code
self.dataTask.resume()
var 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: NSURLSession!,
dataTask: NSURLSessionDataTask!,
didReceiveData: NSData!) {
self.receivedData.appendData(didReceiveData)
//NSLog( "Did receive data" )
var endRange:NSRange = self.receivedData.rangeOfData(self.endMarkerData, options: nil, range: NSMakeRange(0, self.receivedData.length))
// NSRange endRange = [_receivedData rangeOfData:_endMarkerData
// options:0
// range:NSMakeRange(0, _receivedData.length)];
var endLocation = endRange.location + endRange.length
//long long endLocation = endRange.location + endRange.length;
if self.receivedData.length >= endLocation {
var imageData = self.receivedData.subdataWithRange(NSMakeRange(0, endLocation))
var receivedImage = UIImage(data: imageData)
dispatch_async( dispatch_get_main_queue(), {
self.imageView.image = receivedImage
})
//NSLog( "Length: %d", imageData.length )
self.receivedData = NSMutableData(data: self.receivedData.subdataWithRange(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
}
*/
}
@Talha-Ghous
Copy link

hello
thanks for this code
but could you please send me your project that can play ip camera stream by just giving a url ?

@matteocollina
Copy link

i can't start stream from my viewcontroller..can you send me code tnx

@kharitonovAL
Copy link

i need your project too! share it with me please! thx!

@buntsem
Copy link

buntsem commented Sep 1, 2017

i used same logic to extract images from mjpeg file,In my project i am downloading the file from other request and using it whenever required but it is causing memory issues while storing it in the received data, every time I try to access the frames of it. and not releasing it.

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