How is it right to hand a line on the iPhone screen?



  • I'm trying to get the counter. The point is, some line should be placed on a smartphone screen at a speed of personnel, that is, 20 to 30 times a second.

    Realizing this beauty by transferring staff. Somehow:

    CODE UPDATE (1700; 01.07.2016 ICN)

    //.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:&amp;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 gear is working, but it's very slow. The line on the screen shall not be renewed until 10 seconds after the application is launched. Besides, sometimes I start an app, and the line doesn't want to be updated.

    Question: Is there a way to better transfer the line on the smartphone screen? How do you do that?



  • 20 to 30 times a second to update the line for the motto. But it's worth checking how often Output:didOutputSampleBuffer: If he's rarely called, you're not gonna do anything. Add that method to the log.

    UI try to update this (to be exact from the main flow):

    dispatch_async(dispatch_get_main_queue(), ^{
                // Update UI
                self.Label.text = text;
            });
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2