[英]iPhone SDK: Download and Set UIImageView before first view controller is displayed
我無法讓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];
從我的角度來看,您有兩種選擇:
1:在第一次運行時將圖像緩存在Library/Caches
目錄中,並通過某種彈出窗口或幫助視圖來分散用戶的注意力。
2:您可以創建一個較長的啟動屏幕視圖,該視圖僅顯示應用程序的啟動屏幕,直到文件下載完成,然后切換到主視圖。
一般來說,在applicationDidFinishLaunching
方法中執行耗時的代碼不是一個好主意,因為啟動時間太長,您的應用將被終止。
從網絡下載圖像時,您可以顯示一個占位符圖像(來自項目資源),然后在下載后將其替換為正確的圖像。
我還建議您查看SDWebImage框架,它是用於下載和緩存圖像的出色框架。
該庫提供了UIImageVIew的類別,並支持來自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!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.