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

发布于 2024-12-19 14:17:43 字数 1262 浏览 1 评论 0原文

在应用程序完成加载并显示第一个视图控制器后,我无法立即显示 UIImage 。由于图像是从网络下载的,因此图像显示存在延迟。是否有任何方法在 applicationDidFnishLaunching 之前被调用,以便可以立即下载并显示图像?

这是我用来下载图像的代码:
在委托中,在 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];



}

然后在第一个 viewController.m 中:

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

I am having trouble getting a UIImage to display instantly after the app has finished loading and displays the first view controller. 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 ?

Here is the code that I am using to download the images:
In delegate, this method is called in 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 :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-12-26 14:17:43

一般来说,在 applicationDidFinishLaunching 方法中执行耗时的代码并不是一个好主意,因为如果启动时间太长,您的应用程序将被杀死。

您可以在从网络下载图像时显示占位符图像(来自项目资源),然后在下载后将其替换为正确的图像。

我还建议您查看 SDWebImage 框架,这是一个用于下载和缓存图像的出色框架。

该库为 UIImageVIew 提供了一个类别,支持
来自网络的远程图像。

它提供:

UIImageView 类别,向 Cocoa Touch 框架添加 Web 图像和缓存管理
异步图像下载器
具有自动缓存过期处理功能的异步内存+磁盘映像缓存
保证同一个URL不会被下载多次
保证不会一次又一次地重试虚假 URL
表演!

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.

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.

This library provides a category for UIImageVIew with support for
remote images coming from the 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!
只有一腔孤勇 2024-12-26 14:17:43

在我看来,您有两个选择:

1:在第一次运行时将图像缓存在 Library/Caches 目录中,并通过某种弹出窗口或帮助视图来分散用户的注意力。

2:您可以制作一个长时间的启动屏幕视图,它只显示应用程序的启动屏幕,直到文件下载完成,此时您切换到主视图。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文