2016-10-10 50 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

相關問題