iPhone SDK:在显示第一个视图控制器之前下载并设置UIImageView [英] iPhone SDK: Download and Set UIImageView before first view controller is displayed

查看:80
本文介绍了iPhone SDK:在显示第一个视图控制器之前下载并设置UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

以下是我用来下载图像的代码:

在委托中,此方法在appDidFinishLaunching中调用:

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];



}

然后在第一个viewController中.m:

Then in first viewController.m :

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


推荐答案

一般来讲,执行不是一个好主意 applicationDidFinishLaunching 方法中耗时的代码,好像启动时间太长,您的应用将被终止。

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.

我也建议您看看 SDWebImage 框架,它是一个用于下载和缓存图像的好框架。

I also suggest you look at SDWebImage framework, which is a great framework for downloading and caching images.


此库提供UIImageVIew的类别,并支持来自网络的
远程图像。

This library provides a category for UIImageVIew with support for remote images coming from the web.

它提供:

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!


这篇关于iPhone SDK:在显示第一个视图控制器之前下载并设置UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆