Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Created November 1, 2018 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhenakh/43dd49b486a77c4aaf07207f82c8d83d to your computer and use it in GitHub Desktop.
Save akhenakh/43dd49b486a77c4aaf07207f82c8d83d to your computer and use it in GitHub Desktop.
MJPEG player on iOS
#import "MJPEGViewController.h"
@interface MJPEGViewController ()
- (IBAction)reloadTouched:(id)sender;
@property (weak, nonatomic) IBOutlet UIView *connectedView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property( strong, nonatomic) NSMutableData *buffer;
@property(strong, nonatomic) NSURLConnection *conn;
@end
@implementation MJPEGViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void) play {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.12.1:8080/?action=stream"]];
self.conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.buffer = [NSMutableData data];
}
- (void) stop {
[self.conn cancel];
self.connectedView.backgroundColor = [UIColor redColor];
}
- (void) viewWillAppear:(BOOL)animated {
[self play];
}
- (void)viewDidDisappear:(BOOL)animated {
[self stop];
}
- (IBAction)reloadTouched:(id)sender {
[self stop];
[self play];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(nonnull NSError *)error {
self.connectedView.backgroundColor = [UIColor redColor];
self.buffer = nil;
self.imageView.image = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.buffer appendData:data];
self.connectedView.backgroundColor = [UIColor greenColor];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
UIImage *img = [UIImage imageWithData:self.buffer];
self.imageView.image = img;
[self.buffer setLength:0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment