Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@Talha-Ghous Talha-Ghous commented Mar 19, 2016

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

This comment has been minimized.

Copy link

@matteocollina matteocollina commented Jun 26, 2016

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

@kharitonovAL

This comment has been minimized.

Copy link

@kharitonovAL kharitonovAL commented Jan 20, 2017

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

@buntsem

This comment has been minimized.

Copy link

@buntsem 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
You can’t perform that action at this time.