简体   繁体   English

iPhone SDK:在显示第一个视图控制器之前下载并设置UIImageView

[英]iPhone SDK: Download and Set UIImageView before first view controller is displayed

I am having trouble getting a UIImage to display instantly after the app has finished loading and displays the first view controller. 我无法让UIImage在应用程序完成加载并显示第一个视图控制器后立即显示。 There is a delay in the displaying of the image due to it being downloaded from the web. 由于图像是从网络上下载的,因此显示图像存在延迟。 Is there any method that gets called before applicationDidFnishLaunching so the image can be downloaded and displayed instantly ? 在applicationDidFnishLaunching之前是否有任何方法可以调用,以便可以立即下载和显示图像?

Here is the code that I am using to download the images: 这是我用来下载图像的代码:
In delegate, this method is called in appDidFinishLaunching: 在委托中,此方法在appDidFinishLaunching中调用:

-(void)downloadImages {

    NSString *mainImagesJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"mainImagesJSON.php"]]encoding:NSUTF8StringEncoding error:nil];
    SBJsonParser *parser = [[SBJsonParser alloc]init];

    NSDictionary  dictionary1 = [[parser objectWithString:mainImagesJSON error:nil]mutableCopy];


    mainImagesArray = [[dictionary1 valueForKey:@"imgSrc"]mutableCopy];

    NSString *imagesTablePath = [mainImagesArray objectAtIndex:0];
    NSURL *imgURL = [NSURL URLWithString:imagesTablePath];

    NSData *imageData = [NSData dataWithContentsOfURL:imgURL];
    UIImage *image1 = [UIImage imageWithData:imageData];

    imageStorageArray = [[NSMutableArray alloc]init];

    [imageStorageArray addObject:image1];



}

Then in first viewController.m : 然后在第一个viewController.m中:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
UIImage *image1 = [[delegate imageStorageArray]objectAtIndex:0];
[mainHomeImageView setImage:image1];

The way I see it, you have two options: 从我的角度来看,您有两种选择:

1: Cache the image in the Library/Caches directory on the first run, and distract the user with some sort of pop-up, or help view. 1:在第一次运行时将图像缓存在Library/Caches目录中,并通过某种弹出窗口或帮助视图来分散用户的注意力。

2: You could make a prolonged splash screen view, which simply displays the splash screen of your app until the file finishes downloading, at which point you switch to your main view. 2:您可以创建一个较长的启动屏幕视图,该视图仅显示应用程序的启动屏幕,直到文件下载完成,然后切换到主视图。

Generally speaking, it is not a good idea to execute time consuming code in the applicationDidFinishLaunching method as if it takes too long to launch, your app will be killed. 一般来说,在applicationDidFinishLaunching方法中执行耗时的代码不是一个好主意,因为启动时间太长,您的应用将被终止。

You could display a placeholder image (from project resources) while your image downloads from the web, then replace it with the proper image once it's been downloaded. 从网络下载图像时,您可以显示一个占位符图像(来自项目资源),然后在下载后将其替换为正确的图像。

I also suggest you look at SDWebImage framework, which is a great framework for downloading and caching images. 我还建议您查看SDWebImage框架,它是用于下载和缓存图像的出色框架。

This library provides a category for UIImageVIew with support for remote images coming from the web. 该库提供了UIImageVIew的类别,并支持来自Web的远程图像。

It provides: 它提供:

 An UIImageView category adding web image and cache management to the Cocoa Touch framework An asynchronous image downloader An asynchronous memory + disk image caching with automatic cache expiration handling A guarantee that the same URL won't be downloaded several times A guarantee that bogus URLs won't be retried again and again Performances! 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM