2016-10-10 47 views
2

我正在开发一个自定义相机应用程序。我正在做的是,我正在使用相机拍摄照片并将它们显示在屏幕的相同VC底部。如何在Objective-C中使用NSOperation&NSOperationQueue?

我将图像存储在本地字典和NSDocument目录路径中。如果图片在本地字典中,则它将从本地字典中获取,否则将从NSDocument目录路径获取。

收到内存警告后,我只是没有字典,所以它会从NSDocument目录路径中获取图像。

使用两者都会在缓慢的过程中显示图像。我的UI在显示图像方面并没有那么好。

所以我想使用NSOperation将图像存储在NSDocument目录路径中。

我对NSOperation没有太多的知识。我在谷歌搜索,我只是得到快速教程,而我需要在目标C的帮助。

所以,请任何人都可以解释NSOperationNSOperationQueue与例子?

+0

对于教程和其他非现场资源的请求在这里是无关紧要的。为了节省您的问题,我已经编辑了您的问题的脱离主题的要求。你可以随时回滚,如果你想,但如果你这样做,社区将不得不关闭它 – NSNoob

回答

2

应用此每个工作:

 // Allocated here for succinctness. 
     NSOperationQueue *q = [[NSOperationQueue alloc] init]; 

     /* Data to process */ 
     NSData *data = [@"Hello, I'm a Block!" dataUsingEncoding: NSUTF8StringEncoding]; 

     /* Push an expensive computation to the operation queue, and then 
     * display the response to the user on the main thread. */ 
     [q addOperationWithBlock: ^{ 
      /* Perform expensive processing with data on our background thread */ 
      NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 



      /* Inform the user of the result on the main thread, where it's safe to play with the UI. */ 

      /* We don't need to hold a string reference anymore */ 

     }]; 

而且你还可以申请无NSOperationQueue:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      // Your Background work 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       // Update your UI 


      }); 
     }); 

试试更:

  1. NSOperationQueue addOperationWithBlock return to mainQueue order of operations

  2. http://landonf.org/code/iphone/Using_Blocks_1.20090704.html

  3. https://videos.raywenderlich.com/courses/introducing-concurrency/lessons/7

+0

好的。我修改了它。谢谢。请取消,如果你低估了请。@ NSNoob –

+0

感谢Jamshed Alam,实际上我将图像保存在sigleton类的nsdocument目录路径中,在那里我必须放置代码来保存字典。在哪里我必须将这两个代码放在我的单例类中? – kavi

+0

为每个图像应用一个NSOperationQueue。所以代码将会循环。例如:for(;;){NSOperationQueue * op ......整个代码...}。试试吧..希望你能做到。如果你不能这样做,请粘贴一些代码。 –

1

Swift3 创建操作队列

lazy var imgSaveQueue: OperationQueue = { 
    var queue = OperationQueue() 
    queue.name = "Image Save Queue" 
    queue.maxConcurrentOperationCount = 1 
    return queue 
}() 

imgSaveQueue.addOperation(BlockOperation(block: { 
     //your image saving code here 
    })) 

添加操作为目的C:

[[NSOperationQueue new] addOperationWithBlock:^{ 

     //code here 

}]; 
+0

谢谢@Hunaid哈桑,我需要它在objectivec中,请分享一个 – kavi

+0

'[[NSOperationQueue new] addOperationWithBlock:^ { // code这里 }]' –

+0

谢谢@Hunaid哈桑,我需要一个完整的教程,请分享目标c – kavi

相关问题