在iOS中的UIImagePickerController上加载UIActivityIndi​​cator

时间:2012-07-08 20:48:05

标签: objective-c ios uiimage uiimagepickercontroller uiactivityindicatorview

我目前正在编写iOS应用程序,并不喜欢UIImagePickerController消失的速度。

我在[self dismissModalViewControllerAnimated:YES];内即时呼叫- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;然而,imagePicker需要花费一些时间才能消失,在某些照片上已经足够长时间,应用程序会向用户显示冻结状态。

我想到的一个解决方案就是在UIImagePickerController的前面抛出一个UIActivityIndi​​cator,但我还没有找到实现这个目标的方法。

谢谢!

编辑:关于如何更快地保存UIImages的任何提示都有助于我相信。比如一种异步的方法。

1 个答案:

答案 0 :(得分:4)

Ray Wenderlich提供了一个很棒的教程,可以使用大型中央调度和代码块: http://www.raywenderlich.com/1888/how-to-create-a-simple-iphone-app-tutorial-part-33 您也可以使用作业队列和performSelector执行此操作:onThread:withObject:waitUntilDone:if block对您来说是可怕的。

基本上,我们的想法是在主线程上进行最少量的实际工作,因为这是绘制UI的地方。下面是RW的解决方案,状态部分已注释掉。这就是你放置活动指标的地方。

- (IBAction)addPictureTapped:(id)sender {
    if (self.picker == nil) {   

        // 1) Show status
        //[SVProgressHUD showWithStatus:@"Loading picker..."];

        // 2) Get a concurrent queue form the system
        dispatch_queue_t concurrentQueue =
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        // 3) Load picker in background
        dispatch_async(concurrentQueue, ^{

            self.picker = [[UIImagePickerController alloc] init];
            self.picker.delegate = self;
            self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            self.picker.allowsEditing = NO;    

            // 4) Present picker in main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.navigationController presentModalViewController:picker animated:YES];    
                [SVProgressHUD dismiss];
            });

        });        

    }  else {        
        [self.navigationController presentModalViewController:picker animated:YES];    
    }
}