2

在 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

有谁知道这是什么原因造成的?

4

2 回答 2

2

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.

于 2010-09-22T02:29:29.210 回答
0

请注意,这种延迟似乎只发生在 iPhone 4 上:在同一代的 iPod Touch 上,问题几乎没有那么大。

我使用 NSOperation 在后台实例化图像选择器实例。显示/隐藏微调器方法从我正在显示相机图片的图像视图中添加/删除 UIActivityIndi​​catorView。

  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];
 }
于 2011-09-16T05:32:06.213 回答