Created
July 2, 2014 22:48
-
-
Save jeffmcfadden/4970e952466d50eaf533 to your computer and use it in GitHub Desktop.
A UIView Subclass For Displaying An IPCamera Stream (MJPEG)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
*/ | |
} |
i can't start stream from my viewcontroller..can you send me code tnx
i need your project too! share it with me please! thx!
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
hello
thanks for this code
but could you please send me your project that can play ip camera stream by just giving a url ?