Murat Zazi
Murat Zazi

Reputation: 357

Load UIImagePickerController faster?

I need to load UIImagePickerController faster. My app will call UIImagePickerController from possibly multiple controllers and within each controller there are two buttons, one for Photo Library, another for Camera. This is my sample app. Some solutions I tried are recommended by Apple in 'Concurrency Programming Guide'.
The simplest is to alloc & init an instance when user presses a button. This can take up to 2 seconds before the camera shows up.
Solution attempt:-
(1) I made a @property (...) *imagePicker and called its alloc & init in viewDidAppear to make it seem smooth, but it interfered with loading of other elements, possibly because it was using the same thread. Worse results when used in initWithNibName or viewDidLoad.
(2) Operation queues... not pleasing results.
(3) Dispatch Queues... better results but still noticeable choppy performance.
(4) performSelectorInBackground:withObject: not great results. I don't think I want to go global, unless otherwise recommended. I have no problem making two @properties of UIImagePickerController. I will continue to possibly 'Threading Programming Guide'.
Please include any necessary memory management practices with your solutions, if you can. Thank you.

- (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");
}

Upvotes: 1

Views: 2984

Answers (1)

GraehamF
GraehamF

Reputation: 1989

from @Murat's answer in a comment:

I found the solution. This question has already been answered. It seems when connected to Xcode debugger the [UIImagePickerController alloc] init] takes much longer to execute than when running the App without Xcode.

My solution is still to keep @property and do the alloc & init in the viewDidLoad method.

The other solution is here: UIViewController - mysteriously slow to load

moved this to an answer as I almost missed it as a comment.

Upvotes: 5

Related Questions