iPhone SDK:在显示第一个视图控制器之前下载并设置 UIImageView
在应用程序完成加载并显示第一个视图控制器后,我无法立即显示 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,在
applicationDidFinishLaunching
方法中执行耗时的代码并不是一个好主意,因为如果启动时间太长,您的应用程序将被杀死。您可以在从网络下载图像时显示占位符图像(来自项目资源),然后在下载后将其替换为正确的图像。
我还建议您查看 SDWebImage 框架,这是一个用于下载和缓存图像的出色框架。
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.
在我看来,您有两个选择:
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.