我正在尝试在UIActionSheet中单击按钮后显示UIImagePickerController。代码很简单。但是,[[UIImagePickerController alloc] init]
的呼叫在完成之前会挂起几秒。我没有在模拟器中看到这种行为,但我在iPod和iPhone上看到它。
以下是UIActionSheetDelegate方法。添加了日志消息以显示执行时间。
- (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 *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"Action sheet did dismiss with button index %d", buttonIndex);
}
这是实际创建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];
}
}
没有什么花哨的事情发生。但是,如果查看控制台输出,您会注意到创建UIImagePickerController的行需要 7秒才能完成。
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
任何人都知道造成这种情况的原因是什么?
答案 0 :(得分:2)
我在我的应用程序中注意到UIImagePickerController需要很长时间才能创建。解决方法是在需要之前实例化它,即使在单独的线程上,然后在需要时再显示它。我假设但不能确定这是苹果公司如何让它快速合理起来的。
答案 1 :(得分:0)
请注意,这种延迟似乎只发生在iPhone 4上:在同一代的iPod Touch上,它几乎没有问题。
我使用NSOperation在后台实例化图像选择器实例。 show / hide spinner方法在图像视图中添加/删除UIActivityIndicatorView我正在显示相机图片。
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];
}