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

移动这一个答案,我几乎错过了注释。