Trying to implement a counter. The bottom line is that some line should be transmitted to the smartphone screen at a frame rate, that is, 20-30 times per second.
I realize this beauty with the help of frame transfer. Something like this:
CODE UPDATE (5:00 pm; 07/01/2016 EST)
//.h file #import <UIKit/UIKit.h> int N = 0; //Глобальная переменная для подсчета кадров @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *Label; //строка на экране @end //.m file #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController { AVCaptureSession *session; } - (void)viewDidLoad { [super viewDidLoad]; [self setupCaptureSession]; } - (void)setupCaptureSession //инициализация сессии съемки { NSError *error = nil; session = [[AVCaptureSession alloc] init]; session.sessionPreset = AVCaptureSessionPresetMedium; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (!input) { } [session addInput:input]; AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL); //создание очереди кадров [output setSampleBufferDelegate:self queue:queue];// создание буфера сэмплов и добавление очереди [session addOutput:output]; [session startRunning]; } //В методе строкой ниже, осуществляется передача кадров с камеры в буфер - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { NSString *text = [NSString stringWithFormat: @"N = %d", N]; //to create string with global variable N self.Label.text = text; //вывести строку на экран айфона NSLog(text); //распечатать строку в консоль N++; } @end The transfer is obtained, but sooo slow. The line on the screen is updated no earlier than 10 seconds after the launch of the application. And moreover, sometimes I launch the application, and the line does not want to be updated at all.
Question: Is there a way to more effectively transfer the string to the screen of the smartphone? How to do it right?
AVCaptureVideoDataOutputhas a propertysampleBufferCallbackQueuewhere you can specify the stream on which to call your method. pass the main thread there. - Max Mikheyenko