第16月第5天 performSelector afterDelay cancel dispatch_semaphore_wait

1.

            //不延时,可能会导致界面黑屏并卡住一会

            [self performSelector:@selector(startScan) withObject:nil afterDelay:0.3];

 

 

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    

    [NSObject cancelPreviousPerformRequestsWithTarget:self];

 

    [self stopScan];

 

2.ZXCapture

 

    _captureQueue = dispatch_queue_create("com.zxing.captureQueue", NULL);

 

    [_output setSampleBufferDelegate:self queue:_captureQueue];

 

 3.gcd 生产者 消费者

http://blog.csdn.net/jeffasd/article/details/50495977

 

//
//  ViewController.m
//  tick01
//
//  Created by temp on 2018/1/5.
//  Copyright © 2018年 temp. All rights reserved.
//

#import "ViewController.h"

#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;


@interface ViewController ()
{
    dispatch_queue_t    _dispatchQueue;
    
    dispatch_semaphore_t sem;
    
}

@property (readwrite) BOOL                                  playing;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;

@end

@implementation ViewController

- (void)dealloc
{
    _dispatchQueue              = nil;
    
    NSLog(@"111--- ViewController dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _dispatchQueue          = dispatch_queue_create("KxMovie", DISPATCH_QUEUE_SERIAL);
    sem = dispatch_semaphore_create(0);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear
{
    [self pause];
}

#pragma mark - 播放与暂停
- (IBAction)playAndpauseAction:(id)sender
{
    if (self.playing){
        [self state];
    }
    else{
        //[_activityIndicatorView startAnimating];
        [self play];
        [_playBtn setImage:[UIImage imageNamed:@"playback_pause"] forState:UIControlStateNormal];
    }
}

- (void)state
{
    if (self.playing){
        [_playBtn setImage:[UIImage imageNamed:@"playback_play"] forState:UIControlStateNormal];
//        [_activityIndicatorView stopAnimating];
        [self pause];
    }
}

- (void) pause
{
    if (!self.playing)
        return;
    self.playing    = NO;
//    [self enableAudio:NO];
//    [self updatePlayButton];
}



#pragma mark - play
-(void) play
{
    if (self.playing)
        return;

    self.playing        = YES;

    
    [self asyncDecodeFrames];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [self tick];
        
    });
    
    WS(weakSelf);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        while (1) {
            __strong ViewController *strongSelf = weakSelf;
            {
                if (!strongSelf.playing)
                    return;
            }
            
            
            NSLog(@"111--- dispatch_semaphore_wait");
            if (dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, 10 *NSEC_PER_SEC))) {
                continue;
            }
            [strongSelf tick];
        }

    });
}

- (void) tick
{
    CGFloat interval = 0;
    
    if (self.playing) {
        [self asyncDecodeFrames];


        
        NSLog(@"111--- tick");
        


    }
}

- (void) asyncDecodeFrames
{

    WS(weakSelf);

    dispatch_async(_dispatchQueue, ^{
        __strong ViewController *strongSelf = weakSelf;
        {
            if (!strongSelf.playing)
                return;
        }

        NSLog(@"111---do some thing...");
        [NSThread sleepForTimeInterval:3];
        
        
        if (!dispatch_semaphore_signal(sem))
        {
            //                      sleep(1); //wait for a while
            //                      continue;
            NSLog(@"111--- signal fail");
        }
        
        //通知成功
        

    });
}

@end

 gpuimage

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    if (!self.captureSession.isRunning)
    {
        return;
    }
    else if (captureOutput == audioOutput)
    {
        [self processAudioSampleBuffer:sampleBuffer];
    }
    else
    {
        if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0)
        {
            return;
        }
        
        CFRetain(sampleBuffer);
        runAsynchronouslyOnVideoProcessingQueue(^{
            //Feature Detection Hook.
            if (self.delegate)
            {
                [self.delegate willOutputSampleBuffer:sampleBuffer];
            }
            
            [self processVideoSampleBuffer:sampleBuffer];
            
            CFRelease(sampleBuffer);
            dispatch_semaphore_signal(frameRenderingSemaphore);
        });
    }
}

 

 

原文地址:https://www.cnblogs.com/javastart/p/8202865.html