Задать вопрос
  • Получение данных из mjpeg видеопотока и отображение в UIImageView?

    @teddyfox Автор вопроса
    Вот как эта проблема решилась:
    //  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]; }
    ……………………..
    Ответ написан
    Комментировать