4

我有一个 UITableViewController,它弹出一个 UIImagePickerController,用户拍照,点击使用按钮,在处理图像时 Picker 关闭以显示自定义缩略图微调器,然后在处理结束时微调器被实际缩略图替换。

至少它在 iOS4 中是这样工作的。现在使用 iOS5,它只是坐在那里处理直到完成,然后一切正常。但是我想要那个微调器,这样用户就知道发生了什么事,否则它看起来就像挂了。

所以我有这个:

- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex (NSInteger)buttonIndex {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    // yada, yada, yada
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

然后当用户选择“使用”时调用它:

- (void) imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissModalViewControllerAnimated:YES];
    [self performSelectorOnMainThread:@selector(processImage:) withObject:info waitUntilDone:NO];
    animate = true;
}

然后在缩略图旋转时调用它来执行处理:

- (void) processImage:(NSDictionary *)info
{
    UIImage *image = nil;
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    // processing of image
    animate = false;
    [activityImageView stopAnimating];
    [activityImageView release];
    [self.tableView reloadData];
}

就像我说的,它在 iOS4 上完美运行,但在 iOS5 上,就没有这样的运气了。那么有什么关系呢?图像选择器最终会被解散,那么为什么它不立即被解散呢?

4

2 回答 2

4

我不确定为什么iOS4&iOS5在这一点上存在差异。但是您对 UI 挂起的描述与您显示的代码相当一致。主线程上的执行选择器就是这样做的,它在主线程上执行选择器,这是您调用的线程。因为这个设置waitUntilDone:没有NO意义,因为它没有被发送到另一个线程,它只是按顺序运行。您可能会通过交换订单获得所需的结果,例如:

[self dismissModalViewControllerAnimated:YES];
animate = true;
[self performSelectorOnMainThread:@selector(processImage:) withObject:info waitUntilDone:NO];

但请注意,这充其量是有风险的,因为我假设// processing of image不包含并发。我更喜欢并发块。最重要的是,我喜欢嵌套块以使并发易于遵循,例如:

-(void)doSomeStuffInBackground{
    // Prepare for background stuff
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // Do background stuff
        dispatch_async(dispatch_get_main_queue(), ^{
            // Update UI from results of background stuff
        });
    });
}

所以考虑到这一点,我会建议更像这样的东西:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissModalViewControllerAnimated:YES];
    [self processImage:info];
}

-(void)processImage:(NSDictionary *)info{
    animate = true;
    UIImage *image = nil;
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // processing of image here on background thread

        dispatch_async(dispatch_get_main_queue(), ^{
            // update UI here on main thread
            animate = false;
            [activityImageView stopAnimating];
            [activityImageView release];
            [self.tableView reloadData];
        });
    });
}

这会将主要工作卸载到后台线程,以让 UI 保持响应。

于 2011-12-08T04:48:15.603 回答
0

尝试使用

[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil]; 

代替:

[[picker parentViewController] dismissModalViewControllerAnimated: YES];
于 2013-02-24T08:38:48.390 回答