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

  1. //
  2. // ViewController.m
  3. // tick01
  4. //
  5. // Created by temp on 2018/1/5.
  6. // Copyright © 2018年 temp. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. #define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
  12.  
  13. @interface ViewController ()
  14. {
  15. dispatch_queue_t _dispatchQueue;
  16.  
  17. dispatch_semaphore_t sem;
  18.  
  19. }
  20.  
  21. @property (readwrite) BOOL playing;
  22. @property (weak, nonatomic) IBOutlet UIButton *playBtn;
  23.  
  24. @end
  25.  
  26. @implementation ViewController
  27.  
  28. - (void)dealloc
  29. {
  30. _dispatchQueue = nil;
  31.  
  32. NSLog(@"111--- ViewController dealloc");
  33. }
  34.  
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view, typically from a nib.
  38.  
  39. _dispatchQueue = dispatch_queue_create("KxMovie", DISPATCH_QUEUE_SERIAL);
  40. sem = dispatch_semaphore_create();
  41.  
  42. }
  43.  
  44. - (void)didReceiveMemoryWarning {
  45. [super didReceiveMemoryWarning];
  46. // Dispose of any resources that can be recreated.
  47. }
  48.  
  49. - (void)viewWillDisappear
  50. {
  51. [self pause];
  52. }
  53.  
  54. #pragma mark - 播放与暂停
  55. - (IBAction)playAndpauseAction:(id)sender
  56. {
  57. if (self.playing){
  58. [self state];
  59. }
  60. else{
  61. //[_activityIndicatorView startAnimating];
  62. [self play];
  63. [_playBtn setImage:[UIImage imageNamed:@"playback_pause"] forState:UIControlStateNormal];
  64. }
  65. }
  66.  
  67. - (void)state
  68. {
  69. if (self.playing){
  70. [_playBtn setImage:[UIImage imageNamed:@"playback_play"] forState:UIControlStateNormal];
  71. // [_activityIndicatorView stopAnimating];
  72. [self pause];
  73. }
  74. }
  75.  
  76. - (void) pause
  77. {
  78. if (!self.playing)
  79. return;
  80. self.playing = NO;
  81. // [self enableAudio:NO];
  82. // [self updatePlayButton];
  83. }
  84.  
  85. #pragma mark - play
  86. -(void) play
  87. {
  88. if (self.playing)
  89. return;
  90.  
  91. self.playing = YES;
  92.  
  93. [self asyncDecodeFrames];
  94. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
  95. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  96.  
  97. [self tick];
  98.  
  99. });
  100.  
  101. WS(weakSelf);
  102. dispatch_async(dispatch_get_global_queue(, ), ^{
  103.  
  104. while () {
  105. __strong ViewController *strongSelf = weakSelf;
  106. {
  107. if (!strongSelf.playing)
  108. return;
  109. }
  110.  
  111. NSLog(@"111--- dispatch_semaphore_wait");
  112. if (dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, *NSEC_PER_SEC))) {
  113. continue;
  114. }
  115. [strongSelf tick];
  116. }
  117.  
  118. });
  119. }
  120.  
  121. - (void) tick
  122. {
  123. CGFloat interval = ;
  124.  
  125. if (self.playing) {
  126. [self asyncDecodeFrames];
  127.  
  128. NSLog(@"111--- tick");
  129.  
  130. }
  131. }
  132.  
  133. - (void) asyncDecodeFrames
  134. {
  135.  
  136. WS(weakSelf);
  137.  
  138. dispatch_async(_dispatchQueue, ^{
  139. __strong ViewController *strongSelf = weakSelf;
  140. {
  141. if (!strongSelf.playing)
  142. return;
  143. }
  144.  
  145. NSLog(@"111---do some thing...");
  146. [NSThread sleepForTimeInterval:];
  147.  
  148. if (!dispatch_semaphore_signal(sem))
  149. {
  150. //       sleep(1); //wait for a while
  151. //       continue;
  152. NSLog(@"111--- signal fail");
  153. }
  154.  
  155. //通知成功
  156.  
  157. });
  158. }
  159.  
  160. @end

gpuimage

  1. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  2. {
  3. if (!self.captureSession.isRunning)
  4. {
  5. return;
  6. }
  7. else if (captureOutput == audioOutput)
  8. {
  9. [self processAudioSampleBuffer:sampleBuffer];
  10. }
  11. else
  12. {
  13. if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) != )
  14. {
  15. return;
  16. }
  17.  
  18. CFRetain(sampleBuffer);
  19. runAsynchronouslyOnVideoProcessingQueue(^{
  20. //Feature Detection Hook.
  21. if (self.delegate)
  22. {
  23. [self.delegate willOutputSampleBuffer:sampleBuffer];
  24. }
  25.  
  26. [self processVideoSampleBuffer:sampleBuffer];
  27.  
  28. CFRelease(sampleBuffer);
  29. dispatch_semaphore_signal(frameRenderingSemaphore);
  30. });
  31. }
  32. }

第16月第5天 performSelector afterDelay cancel dispatch_semaphore_wait的更多相关文章

  1. 第16月第31天 mongo

    1. 94  brew install  mongodb 95  cd ~ 96  cd Desktop/web/ 97  ls 98  mkdir mongo 99  cd mongo/ 100  ...

  2. 第16月第27天 pip install virtualenv ipython sip brew search

    1. pip install virtualenv virtualenv testvir cd testvir cd Scripts activate pip https://zhuanlan.zhi ...

  3. 第16月第26天 /bin/bash^M: bad interpreter: 没有那个文件或目录

    1. 运行脚本时出现了这样一个错误,打开之后并没有找到所谓的^M,查了之后才知道原来是文件格式的问题,也就是linux和windows之间的不完全兼容...具体细节不管,如果验证: vim test. ...

  4. 第16月第25天 tableView设置UITableViewStyleGrouped顶部有空余高度

    1. 正确的处理方法 1)设置标头的高度为特小值 (不能为零 为零的话苹果会取默认值就无法消除头部间距了) UIView *view = [[UIView alloc]initWithFrame:CG ...

  5. 第16月第24天 find iconv sublime utf-8

    1. find . -type f -exec echo {} \; find src -type f -exec sh -c "iconv -f GB18030 -t UTF8 {} &g ...

  6. 第16月第23天 atos

    1. grep --after-context=2 "Binary Images:" *crash xcrun atos -o zhiniao_adhoc_stg1.app.dSY ...

  7. 第16月第17天 contentMode

    1. self.contentMode = UIViewContentModeScaleAspectFill; self.clipsToBounds = YES; http://blog.csdn.n ...

  8. 第16月第15天 glut

    1. https://tokoik.github.io/opengl/libglut.html https://github.com/wistaria/wxtest/tree/master/C htt ...

  9. 第16月第12天 CABasicAnimation 旋转加速

    1. ; double duration = 10.0f; ; i<count; i++) { //旋转动画 CABasicAnimation *anima3 = [CABasicAnimati ...

随机推荐

  1. Java之枚举类范例

    代码如下: package catf.component.http.model; /** * @Auther:gongxingrui * @Date:2018-04-17 * @Description ...

  2. python之Map函数

    # map()函数使用举例 # 功能:map()接受一个函数f和一个或多个list,将f依次作用在list的每个元素,得到一个新的列表 # 语法:map(方法名,列表,[列表2]) # 注意:map( ...

  3. SQL Server 公用表表达式(CTE)实现递归

    公用表表达式简介: 公用表表达式 (CTE) 可以认为是在单个 SELECT.INSERT.UPDATE.DELETE 或 CREATE VIEW 语句的执行范围内定义的临时结果集.CTE 与派生表类 ...

  4. 画caffe训练loss曲线

    Linux下操作 1. 将loss值存储到lossInf.txt中 fName1='loss.txt' cat loss.log | grep "solver.cpp:218] Iterat ...

  5. IBM推出新一代云计算技术来解决多云管理

    IBM 云计算论坛在南京举行,推出了一项全新的开放式技术,使用户能够更加便捷地跨不同云计算基础架构来管理.迁移和整合应用. IBM 多云管理解决方案(Multicloud Manager)控制面板 据 ...

  6. 最小费用最大流spfa

    #include <iostream> #include <cstring> #include <cstdio> #include <queue> #d ...

  7. day7 笔记

    二进制-----> ASCLL :只能存英文和拉丁字符.-----> gb2312 :只有6700来个中文字符,1980年-----> gbk1.0 :存了2w多字符 ,1995年- ...

  8. MT【30】椭圆的第二定义解题

    问题:上式表示的区域是怎样的? 解答:利用椭圆第二定义易知当取等号时为椭圆,又令$y$趋向于$+\infty$时不等号不成立,故可以判断为椭圆内部区域. 评:利用mathmatics软件容易得到

  9. copy elison & RVO & NRVO

    蓝色的博文 To summarize, RVO is a compiler optimization technique, while std::move is just an rvalue cast ...

  10. 自学Python1.6-Centos内英文语法切换

    自学Python之路 自学Python1.6-Centos内中英文语法切换 测试采用是官方Centos7.0系统 1.添加中文输入法 打开设置---区域和语言--->在输入源最下面有一个 + 号 ...