在 ios 中从服务器下载图像时显示启动画面

displaying splash screen while downloading images from server in ios

在我的应用程序中,我尝试使用 LWSlideShow 制作滑块 LWSlideShow 图像源是在尝试解决方案后从我的服务器获取的 here 我坚持说不平衡调用的错误,这意味着我在解决此问题后未完成动画的视图上呈现模态视图将动画设置为 no 我呈现的 splashView 将在下载图像之前被关闭这里是我的代码以供进一步解释:

 - (IBAction)goDownload {

    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Splash"];
        [self.navigationController presentViewController:vc animated:YES completion:nil];

    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableArray *array = [@[] mutableCopy];
        LWSlideItem *item = [LWSlideItem itemWithCaption:@""
                                                imageUrl:@"http://code-bee.net/geeks/images/cover-1.jpg"];
        [array addObject:item];
        item = [LWSlideItem itemWithCaption:@""
                                   imageUrl:@"http://code-bee.net/geeks/images/cover-2.jpg"];
        [array addObject:item];
        item = [LWSlideItem itemWithCaption:@""
                                   imageUrl:@"http://code-bee.net/geeks/images/cover-3.jpg"];
        [array addObject:item];





        LWSlideShow *slideShow = [[LWSlideShow alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 120)];

        slideShow.autoresizingMask = UIViewAutoresizingFlexibleWidth;

        //slideShow.delegate = self;
        [self.view addSubview:slideShow];
        slideShow.slideItems = array;



        if ([slideShow.slideItems count] == [array count]) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }



    });









}





//
//-(void)viewWillAppear:(BOOL)animated
//{
//
//    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Splash"];
//    [self.navigationController presentViewController:vc animated:YES completion:nil];
//}




- (void)viewDidLoad {
    [super viewDidLoad];




    [self goDownload];
    }

你也可以从代码中看到我也尝试使用 viewWillAppear 发生了同样的事情我想要的是当图像被下载时需要关闭 splashView 我不知道我做错了什么

运行 在 vi​​ewDidAppear(如 viewDidLoad、viewWillAppear)之前的任何时间来自 VC 的代码将导致您描述的问题。但是您可能不希望幻灯片放映视图出现——即使是一瞬间——直到您完成获取资产。这是一个常见问题。

解决方案是认识到 "splash screen" 和网络任务不仅仅是序言,它们与幻灯片放映一样是应用程序的一部分。

编辑 使 Splash vc 成为故事板中应用程序的初始视图控制器。现在,幻灯片 vc 可能看起来像这样:

取消选中 "Is Initial View Controller" 复选框,找到您的初始视图控制器(我希望在同一个故事板中)并选中它作为初始视图控制器的框。现在,您的应用程序将启动 vc,如您所愿。

启动 vc 完成后,它可以放映幻灯片 vc,或者它甚至可以将自己替换为应用程序 window 的根目录(使用幻灯片放映) .

为了替换 UI,我使用了此代码段的变体...

// in the splash vc, after all of the asset loading is complete

// give what used to be your initial view controller a storyboard id
// like @"MySlideShowUI"

UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MySlideShowUI"];

UIWindow *window = [UIApplication sharedApplication].delegate.window;
window.rootViewController = vc;

[UIView transitionWithView:window
                  duration:0.3
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:nil
                completion:nil];