0

我需要加載UIImagePickerController更快。我的應用程序將從可能的多個控制器呼叫UIImagePickerController,並且在每個控制器內有兩個按鈕,一個用於照片庫,另一個用於相機。這是我的示例應用程序。我嘗試過的一些解決方案是Apple在'併發編程指南'中推薦的。
最簡單的是當用戶按下一個按鈕時,將一個實例分配給&。相機出現之前最多可能需要2秒。
解決方案嘗試: -
(1)我創建了一個@property(...)* imagePicker,並調用其&初始值viewDidAppear以使其看起來平滑,但它干擾了其他元素的加載,可能是因爲它正在使用同一個線程。當在initWithNibNameviewDidLoad中使用時會導致較差的結果。
(2)Operation queues ...不令人滿意的結果。
(3)Dispatch Queues ...更好的結果,但仍然明顯的波濤洶涌的表現。
(4)performSelectorInBackground:withObject:不是很好的結果。除非另有建議,否則我不認爲我想走向世界。我有兩個@properties UIImagePickerController沒有問題。我將繼續可能'線程編程指南'。
如果可以的話,請在您的解決方案中包含任何必要的內存管理實踐。謝謝。加載UIImagePickerController速度更快?

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
// (1) TRYING OPERATION QUEUES 
    // (a) --- NSBlockOperation 
    NSBlockOperation *NSBO_IP = [NSBlockOperation blockOperationWithBlock:^{ 
     NSLog(@"before"); 
     imagePicker = [[UIImagePickerController alloc] init]; 
     // 1.9, 1.6, 1.4 seconds pass between 'before' and 'after' 
     // it seems this method has a dynamic technique, executes in different order every time 
     NSLog(@"after"); 
    }]; 
    // (b) --- NSInvocationOperation 
    NSInvocationOperation *NSIO_IP = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadTheImagePicker) object:nil]; 
    // --- Add an operation 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    //NSLog(@"time 1"); 
    // (a) [queue addOperation:NSBO_IP]; 
    // (b) [queue addOperation:NSIO_IP]; 
    //NSLog(@"time 2"); 
// (2)TRYING DISPATCH QUEUES 
    // (a) --- GCD call (do I need to release 'myQueue' at some point since this is C-level call?) 
/* 
    NSLog(@"time 1"); 
    dispatch_queue_t myQueue = dispatch_queue_create("My Queue", NULL); 
    dispatch_async(myQueue, ^{ 
     imagePicker = [[UIImagePickerController alloc] init]; 
     // 1.2, 1.2, 1.2, 1.4 seconds significant increase over Operation Queues 
     // a solid constant executing technique, executes very consistently 
    }); 
    NSLog(@"time 2"); 
*/ 
// (3)TRYING performSelectorInBackground:withObject (not recommended?) 
    NSLog(@"time 1"); 
    [self performSelectorInBackground:@selector(loadTheImagePicker) withObject:nil]; 
    // 1.3, 1.7, 1.3 seconds pass between 'before' and 'after' 
    NSLog(@"time 2"); 
// RESULTS REFLECTED IN LINE BELOW ! 
    [self changeText:self]; // text does not change on time when trying to alloc & init an ImagePickerController 
} 
    - (void)loadTheImagePicker 
    { 
    NSLog(@"before"); 
    imagePicker = [[UIImagePickerController alloc] init]; 
    // --- NSInvocationOperation used 
    // 1.6, 1.2, 1.4 seconds pass between 'before' and 'after' 
    // this method has a more constant executing technique, as far as order of executions 
    // --- NSInvocationOperation used 
    NSLog(@"after"); 
} 
+0

不建議在後臺線程上執行UI更改,它變得不可預知。那麼你是否嘗試過沒有任何線程? – Emanuel

+0

通常,視圖將在'UIViewController'被添加到導航堆棧之後加載(這種或那種方式)。我會**不推薦,但你可以調用'-loadView'方法_before_它將被添加;它爲你節省了一點時間,但是這樣調用'-loadView'方法的行爲是未定義的,所以這是一個明確的風險。注意:_initialising_視圖不等於_loading_視圖(及其組件)。 – holex

+0

是的,伊曼紐爾,我已經嘗試過沒有任何線程。它需要最長的時間來加載。 –

回答

3

從@穆拉特在評論答案:

我找到了解決辦法。這個問題已經得到解答。它連接到Xcode調試器時似乎 [UIImagePickerController alloc] init]比在沒有Xcode的情況下運行App 花費的時間要長得多。

我的解決辦法仍然是保持@property並在viewDidLoad方法做alloc & init

另一種方案是在這裏: UIViewController - mysteriously slow to load

移動這一個答案,我幾乎錯過了註釋。