【问题标题】:Adding a UIActivityIndicator to a modal view (ELCimagepicker)将 UIActivityIndi​​cator 添加到模态视图 (ELCimagepicker)
【发布时间】:2011-05-28 09:45:15
【问题描述】:

我已将 ELCimagepicker (https://github.com/Fingertips/ELCImagePickerController) 添加到我的项目中,它运行良好,允许用户为幻灯片选择多个图像。但是,当您点击“保存”时,可能会有很长的延迟,具体取决于添加的照片数量。

我一直在尝试在用户单击“保存”时添加 UIActivityIndi​​cator,但由于显示的模式视图而遇到问题。我可以从 ELCimagepicker 呈现的活动 (ELCImagePickerController) 中调用一个方法,这将由处理图像选取器呈现的活动执行。但是每当我尝试添加到视图时,它都不会显示,因为模式位于活动指示器的顶部。

我尝试过使用bringSubviewToFront,我尝试过使用[[self parentViewController] addSubView] 将代码直接添加到imagepicker 方法文件中,但没有成功。

这是我尝试过的最新代码:(指标在 .h 文件中声明为 UIActivityIndi​​cator *indicator)

 indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.hidden=false;

[self.navigationController.view addSubview:self.indicator];
[self.navigationController.view bringSubviewToFront:self.indicator];

[indicator startAnimating];

if([delegate respondsToSelector:@selector(elcImagePickerController:showIndicator:)]) {
    [delegate performSelector:@selector(elcImagePickerController:showIndicator:) withObject:self withObject:@"test"];
}

有没有人在 ELCimagepicker 之上添加 UIActivityIndi​​cator 或由另一个类处理的另一个模态视图取得任何成功?

我已经尝试过 MBProgressHUD,但也无法让它正常工作 - 当我在 ELCimagepicker 类中使用它时它会显示出来,但在删除时崩溃:

bool _WebTryThreadLock(bool), 0x42368e0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

任何帮助都会很棒。

谢谢。

【问题讨论】:

  • 你能贴出工作代码吗?
  • @toddl 您能否发布您的工作代码或指导,因为我在放置指标时遇到了类似的问题。而且我对 performSelectorOnMainThread 以及如何使其工作也不太熟悉。
  • @toddl 您好,请您发布有用的代码。你是怎么解决的,因为我仍然无法解决。有什么指导方针吗?

标签: iphone modalviewcontroller uiactivityindicatorview addsubview


【解决方案1】:

我已经解决了你的问题。您可以按以下方式执行此操作..

-(void)selectedAssets:(NSArray*)_assets {

UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIViewController * topView = [self.viewControllers objectAtIndex:[self.viewControllers count]-1];
activityIndicator.center = CGPointMake(topView.view.frame.size.width/2, topView.view.frame.size.height/2);
[activityIndicator setHidden:NO];
[topView.view addSubview:activityIndicator];
[topView.view bringSubviewToFront:activityIndicator];
[activityIndicator startAnimating];

[self performSelector:@selector(doProcess:) withObject:_assets afterDelay:2.1];

}

- (void) doProcess:(NSArray *)_assets {

NSMutableArray *returnArray = [[[NSMutableArray alloc] init] autorelease];

for(ALAsset *asset in _assets) {

    NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
    [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
    [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
    [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];

    [returnArray addObject:workingDictionary];

    [workingDictionary release];    
}

[self popToRootViewControllerAnimated:NO];
[[self parentViewController] dismissModalViewControllerAnimated:YES];

if([delegate respondsToSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:)]) {
    [delegate performSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:) withObject:self withObject:[NSArray arrayWithArray:returnArray]];
}

}

如果这个答案对你有帮助,请告诉我...

谢谢, MinuMaster

【讨论】:

    【解决方案2】:

    看起来您正在后台线程上更新 UI。所有 UIKit 更新都将在主线程中完成。因此,我建议您使用performSelectorOnMainThread:withObject: 执行 UI 更新的方法。

    【讨论】:

    • 谢谢 - 我现在已经实现了对主类的调用 [delegate performSelectorOnMainThread:@selector(elcImagePickerController:) withObject:self waitUntilDone:YES];这会在我的主类上调用并显示活动指示器 - 但仅在模式视图完成之后。我想我可能错误地将它添加到视图中 - 我尝试了 [self.view addSubview:activityIndi​​cator],但后来才显示,所以我尝试了 self.navigationController.view 和 visibleViewController、topViewController 等... 怎么做我告诉它在模态视图的“顶部”?
    • 您可以存储对模态视图控制器的引用,并在调用委托方法时将activityIndicator 添加到其子视图,否则您可以使用通知。
    • 模态视图控制器被称为 elcPicker,当它在主类中被回调时,我会这样做,从带有 performSelectorOnMainThread 的 imagepicker 类,使用 [elcPicker.view addSubview:activityIndi​​cator],但这是当它没有出现。
    • 谢谢 - 我现在已经结合了 performSelectorOnMainThread 并将 elcImagePicker 类更改为 performSelectorOnBackgroundThread。
    • @toddl 您能否发布您的工作代码或指导,因为我在放置指标时遇到了类似的问题。而且我对 performSelectorOnMainThread 以及如何使其工作也不太熟悉。
    【解决方案3】:

    我解决了这样的问题

         activityIndicatorObject = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
         // Set Center Position for ActivityIndicator
    
         activityIndicatorObject.center = CGPointMake(150, 250);
         activityIndicatorObject.backgroundColor=[UIColor grayColor];
    
         // Add ActivityIndicator to your view
         [self.view addSubview:activityIndicatorObject];
    
         activityIndicatorObject.hidesWhenStopped=NO;
    
         [activityIndicatorObject startAnimating];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多