T
T
teddyfox2015-01-28 20:40:04
iOS
teddyfox, 2015-01-28 20:40:04

Getting data from mjpeg video stream and displaying in UIImageView?

I'm trying to get a mjpeg video stream from an IP camera and display it in an ios application. The connection with the IP camera via NSURLConnection occurs, the procedures didReceiveData and didReceiveResponse are entered (set and displayed debug counters), but the display in the UIImageView (created in IB) does not occur. Plus a small memory leak. Tell me, please, where is the hitch? I checked both in iOSSimulator'e and by downloading to the iPad. I have little programming experience in XCode (6.1.1). Thank you.
Here is the code:

//  ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *cameraView;
@property (strong) IBOutlet UILabel *label;
@end

//  ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
@synthesize cameraView;
@synthesize label;

    NSMutableData *dataResponse;     // buffer for accumulating data

- (void)viewDidLoad
{   [super viewDidLoad];
    dataResponse = [NSMutableData dataWithCapacity:100];
   NSString *cameraURL = @"http://192.168.1.160:554/video.mjpg"; // URL request
    NSURL *url = [NSURL URLWithString:cameraURL];
    NSURLRequest *requestURLcamera = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestURLcamera delegate:self];	// connecting
    if (!connection)
        {   dataResponse = nil; 		// if no connection - release dataResponse
            self.label.text = @"No connection!";}
    else { self.label.text = @"Connection OK!"; };
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}

- (void)connection: (NSURLConnection *) connection didReceiveData:(NSData *)data
{    [dataResponse appendData:data]; }	// accumulating data

- (void)connection: (NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
   @autoreleasepool  
  { 
    UIImage *imageZ = [UIImage imageWithData:dataResponse];     // creating image
    self.cameraView.image = imageZ;  	// image to UIImageView
  }
   [dataResponse setLength:0];
}
@end

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
T
teddyfox, 2015-04-16
@teddyfox

Here is how this problem was solved:

//  ViewController.m
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <CFNetwork/CFNetwork.h>

@interface ViewController ()
@end

@implementation ViewController

NSMutableData *_receivedData;	// buffer for accumulating data
#define END_MARKER_BYTES { 0xFF, 0xD9 }
static NSData *_endMarkerData = nil;

……………………….

- (IBAction)getVideo:(id)sender	// прием видеопотока по кнопке
{   NSString *finalURLcamera = @"http://"; NSString *urlIP = [_cameraIP text];	// IP-адрес камеры - в TextField'e cameraIP
    finalURLcamera = [finalURLcamera stringByAppendingString:urlIP];                // конструируем полный URL
    finalURLcamera = [finalURLcamera stringByAppendingString:@":554/video.mjpg"];
    
    NSURL *url = [NSURL URLWithString:finalURLcamera];
    NSURLRequest *requestURLcamera = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestURLcamera delegate:self];	// connecting
    
    uint8_t endMarker[2] = END_MARKER_BYTES;
    _endMarkerData = [[NSData alloc] initWithBytes:endMarker length:2];
}

………………………………….

// ****************** прием видеопотока ****************
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   if (!_receivedData) { _receivedData = [NSMutableData new]; }
    [_receivedData appendData:data];
    NSRange endRange = [_receivedData rangeOfData:_endMarkerData options:0
                                            range:NSMakeRange(0, _receivedData.length)];
    if (endRange.location == NSNotFound) { return; }
    @autoreleasepool
    {  UIImage *receivedImage = [UIImage imageWithData:_receivedData];
        if (receivedImage) { self.cameraWindow.image = receivedImage; }	// cameraWindow - это UIImageView
    }
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{    _receivedData = [[NSMutableData alloc] init]; }
……………………..

S
Sergey Lerg, 2015-01-28
@Lerg

Are you processing the input stream in any way? There is also a protocol from which you need to pick out JPEG images. You can read my article at least habrahabr.ru/post/115808

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question