UIImagePickerController creation hangs for several seconds
I'm trying to show a UIImagePickerController after a button is clicked in an UIActionSheet. The code is straightforward. However, the call to [[UIImagePickerController alloc] init]
is hanging for several seconds before it finishes. I don't see this behavior in the simulator, but am seeing it on an iPod and iPhone.
Here are the UIActionSheetDelegate methods. Log messages were added to show execution times.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Action sheet clicked button at index %d", buttonIndex);
switch (buttonIndex) {
case kSelectFromCameraButtonIndex:
[self showImagePickerWithCamera];
break;
case kSelectFromPhotoLibraryButtonIndex:
[self showImagePickerWithPhotoLibrary];
break;
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"Action sheet will dismiss with button index %d", buttonIndex);
}
- (void)actionSheet:(UIActionSheet *)actio开发者_如何学PythonnSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"Action sheet did dismiss with button index %d", buttonIndex);
}
And here's the code that actually creates the UIImagePickerController:
- (void)showImagePickerWithPhotoLibrary {
NSLog(@"Showing image picker with photo library");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
NSLog(@"Creating picker");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
NSLog(@"Setting picker settings");
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSLog(@"Presenting picker as modal view controller");
[self presentModalViewController:picker animated:YES];
NSLog(@"Releasing picker");
[picker release];
}
}
Nothing fancy going on. However, if you look at the console output, you'll notice that the line where the UIImagePickerController is created takes about 7 seconds to finish.
2010-09-21 15:23:26.107 Oh Snap[1264:307] Action sheet clicked button at index 1
2010-09-21 15:23:26.113 Oh Snap[1264:307] Showing image picker with photo library
2010-09-21 15:23:26.120 Oh Snap[1264:307] Creating picker
2010-09-21 15:23:33.111 Oh Snap[1264:307] Setting picker settings
2010-09-21 15:23:33.123 Oh Snap[1264:307] Presenting picker as modal view controller
2010-09-21 15:23:33.136 Oh Snap[1264:307] Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
2010-09-21 15:23:33.144 Oh Snap[1264:307] Using two-stage rotation animation is not supported when rotating more than one view controller or view controllers not the window delegate
2010-09-21 15:23:33.289 Oh Snap[1264:307] Releasing picker
2010-09-21 15:23:33.299 Oh Snap[1264:307] Action sheet will dismiss with button index 1
2010-09-21 15:23:33.916 Oh Snap[1264:307] Action sheet did dismiss with button index 1
Anyone know what's causing this?
I have noticed in my applications that UIImagePickerController takes a long time to create. A workaround is to instantiate it before you need it, even on a separate thread, and then present it later when it is needed. I assume but can't confirm that this is how Apple makes it come up reasonably fast.
Note that this delay only seems to happen on the iPhone 4: on the iPod Touch of the same generation it's not nearly as big of a problem.
I use a NSOperation to instantiate the image picker instance in the background. The show/hide spinner methods add/remove a UIActivityIndicatorView from the image view I'm showing the camera picture in.
NSOperationQueue *operations = ...
...
[self showSpinner];
[operations addOperation:
[NSBlockOperation blockOperationWithBlock:
^{
UIImagePickerController *cameraUI = [UIImagePickerController new];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
[self performSelectorOnMainThread: @selector (showImagePicker:)
withObject: cameraUI waitUntilDone: YES];
}]];
...
- (void) showImagePicker: (UIImagePickerController *) picker
{
[self hideSpinner];
[self presentModalViewController: picker animated: YES];
}
精彩评论