更快地加载 UIImagePickerController?

Mur*_*azi 1 multithreading memory-management uiimagepickercontroller grand-central-dispatch ios

我需要UIImagePickerController更快地加载。我的应用程序UIImagePickerController可能会从多个控制器调用,并且在每个控制器中有两个按钮,一个用于照片库,另一个用于相机。这是我的示例应用程序。Apple 在“并发编程指南”中推荐了我尝试过的一些解决方案。
最简单的方法是在用户按下按钮时分配和初始化一个实例。这最多可能需要 2 秒才能显示相机。
解决方案尝试:-
(1) 我创建了一个@property (...) *imagePicker 并调用了它的 alloc & initviewDidAppear以使其看起来很流畅,但它干扰了其他元素的加载,可能是因为它使用了相同的线程。在initWithNibName或 中使用时结果更糟viewDidLoad
(2) Operation queues... 不令人满意的结果。
(3) Dispatch Queues... 更好的结果,但仍然明显不稳定的性能。
(4)performSelectorInBackground:withObject:结果不大。我不认为我想走向全球,除非另有建议。我制作两个 @properties 没有问题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");
}
Run Code Online (Sandbox Code Playgroud)

Gra*_*amF 5

来自@Murat 在评论中的回答:

我找到了解决方案。这个问题已经有了答案。似乎当连接到 Xcode 调试器[UIImagePickerController alloc] init]时,执行时间比在没有 Xcode 的情况下运行应用程序时要长得多。

我的解决方案仍然是在方法中保留@property并执行alloc & 。initviewDidLoad

另一个解决方案在这里: UIViewController - 加载速度很慢

将其移至答案,因为我几乎错过了它作为评论。