• <i id='fPsYa'><tr id='fPsYa'><dt id='fPsYa'><q id='fPsYa'><span id='fPsYa'><b id='fPsYa'><form id='fPsYa'><ins id='fPsYa'></ins><ul id='fPsYa'></ul><sub id='fPsYa'></sub></form><legend id='fPsYa'></legend><bdo id='fPsYa'><pre id='fPsYa'><center id='fPsYa'></center></pre></bdo></b><th id='fPsYa'></th></span></q></dt></tr></i><div id='fPsYa'><tfoot id='fPsYa'></tfoot><dl id='fPsYa'><fieldset id='fPsYa'></fieldset></dl></div>
  • <legend id='fPsYa'><style id='fPsYa'><dir id='fPsYa'><q id='fPsYa'></q></dir></style></legend>
    <tfoot id='fPsYa'></tfoot>
      <bdo id='fPsYa'></bdo><ul id='fPsYa'></ul>

    1. <small id='fPsYa'></small><noframes id='fPsYa'>

        使用 Grand Central Dispatch 时如何发布 NSNotification?

        时间:2023-10-23
            <tbody id='3npjR'></tbody>

          <i id='3npjR'><tr id='3npjR'><dt id='3npjR'><q id='3npjR'><span id='3npjR'><b id='3npjR'><form id='3npjR'><ins id='3npjR'></ins><ul id='3npjR'></ul><sub id='3npjR'></sub></form><legend id='3npjR'></legend><bdo id='3npjR'><pre id='3npjR'><center id='3npjR'></center></pre></bdo></b><th id='3npjR'></th></span></q></dt></tr></i><div id='3npjR'><tfoot id='3npjR'></tfoot><dl id='3npjR'><fieldset id='3npjR'></fieldset></dl></div>

          <small id='3npjR'></small><noframes id='3npjR'>

          • <bdo id='3npjR'></bdo><ul id='3npjR'></ul>

          • <tfoot id='3npjR'></tfoot>

          • <legend id='3npjR'><style id='3npjR'><dir id='3npjR'><q id='3npjR'></q></dir></style></legend>

                  本文介绍了使用 Grand Central Dispatch 时如何发布 NSNotification?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我发现正如我在将图像写入文件时所预测的那样,我的 UI 在此期间被阻止,这是不可接受的.当我将图像写入文件时,我会发布一个 NS 通知,以便我可以做一些与该完成相关的其他特定工作.原始工作但 UI 阻塞代码:

                  I found that as predicted when I was writing an image to file that my UI was blocked for the duration, which was not acceptable. When I write the image to file I then post an NS Notification so that I can do some other specific jobs related to that completion. Original working but UI blocking code:

                  -(void)saveImageToFile {
                      NSString *imagePath = [self photoFilePath];
                      BOOL jpgData = [UIImageJPEGRepresentation([[self captureManager] stillImage], 0.5) writeToFile:imagePath atomically:YES];
                  
                  if (jpgData) {        
                      [[NSNotificationCenter defaultCenter] postNotificationName:kImageSavedSuccessfully object:self];
                  }
                  

                  为避免 UI 阻塞,我将 writeToFile: 放入 Grand Central Dispatch 队列中,以便它作为并发线程运行.但是当写入完成并且线程完成时,我想发布一个 NSNotification.我不能,因为这里显示了代码,因为它在后台线程中.但这是我想要完成的功能,意识到这不是可行的代码:

                  To avoid the UI blocking I have put the writeToFile: into a Grand Central Dispatch queue so it runs as a concurrent thread. But when the write is completed and the thread is done, I want to post an NSNotification. I cannot as the code is shown here because it is in a background thread. But that is the functionality I want to accomplish, realizing this is not workable code:

                  -(void)saveImageToFile {
                      NSString *imagePath = [self photoFilePath];
                  
                      // execute save to disk as a background thread
                      dispatch_queue_t myQueue = dispatch_queue_create("com.wilddogapps.myqueue", 0);
                      dispatch_async(myQueue, ^{
                          BOOL jpgData = [UIImageJPEGRepresentation([[self captureManager] stillImage], 0.5) writeToFile:imagePath atomically:YES];
                          dispatch_async(dispatch_get_main_queue(), ^{
                              if (jpgData) {        
                              [[NSNotificationCenter defaultCenter] postNotificationName:kImageSavedSuccessfully object:self];
                              }
                          });
                      });
                  }
                  

                  在这里发布此通知以获得我想要的功能的正确机制是什么?

                  What is the correct mechanism here to post this notification to gain the functionality I want ?

                  推荐答案

                  这里有几个可能性.

                  1)

                  [NSObject performSelectorOnMainThread: ...] 怎么样?

                  How about [NSObject performSelectorOnMainThread: ...] ?

                  例如

                  -(void) doNotification: (id) thingToPassAlong
                  {
                      [[NSNotificationCenter defaultCenter] postNotificationName:kImageSavedSuccessfully object:thingToPassAlong];
                  }
                  
                  -(void)saveImageToFile {
                      NSString *imagePath = [self photoFilePath];
                  
                      // execute save to disk as a background thread
                      dispatch_queue_t myQueue = dispatch_queue_create("com.wilddogapps.myqueue", 0);
                      dispatch_async(myQueue, ^{
                          BOOL jpgData = [UIImageJPEGRepresentation([[self captureManager] stillImage], 0.5) writeToFile:imagePath atomically:YES];
                          dispatch_async(dispatch_get_main_queue(), ^{
                              if (jpgData) {   
                                  [self performSelectorOnMainThread: @selector(doNotification:) withObject: self waitUntilDone: YES];
                              }
                          });
                      });
                  }
                  

                  更多详情请访问 http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone:

                  或 2)

                  完成回调

                  如 如何当 dispatch_async 任务完成时我会收到通知?

                  这篇关于使用 Grand Central Dispatch 时如何发布 NSNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Resize UIImagePickerController 视频采集界面 下一篇:为什么 UIView 框架是由浮动组成的?

                  相关文章

                  <legend id='lhSZ9'><style id='lhSZ9'><dir id='lhSZ9'><q id='lhSZ9'></q></dir></style></legend>
                      <bdo id='lhSZ9'></bdo><ul id='lhSZ9'></ul>

                      <i id='lhSZ9'><tr id='lhSZ9'><dt id='lhSZ9'><q id='lhSZ9'><span id='lhSZ9'><b id='lhSZ9'><form id='lhSZ9'><ins id='lhSZ9'></ins><ul id='lhSZ9'></ul><sub id='lhSZ9'></sub></form><legend id='lhSZ9'></legend><bdo id='lhSZ9'><pre id='lhSZ9'><center id='lhSZ9'></center></pre></bdo></b><th id='lhSZ9'></th></span></q></dt></tr></i><div id='lhSZ9'><tfoot id='lhSZ9'></tfoot><dl id='lhSZ9'><fieldset id='lhSZ9'></fieldset></dl></div>

                      <small id='lhSZ9'></small><noframes id='lhSZ9'>

                    1. <tfoot id='lhSZ9'></tfoot>