2013-08-02 45 views
0

我試圖從網絡與幾個簡單的步驟加載數據:IOS應用程序加載數據從網絡

NSData *JSONData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"******"]]; 

NSObject *json = [JSONData objectFromJSONData]; 
NSArray *arrayOfStreams = [json valueForKeyPath:@"programs"]; 
NSDictionary *stream = [arrayOfStreams objectAtIndex:0]; 
NSString *str = [[NSString alloc]initWithString:[stream valueForKey:@"image"]]; 
NSURL *urlForImage1 = [NSURL URLWithString:str]; 
NSData *imageData1 = [NSData dataWithContentsOfURL:urlForImage1]; 
_screenForVideo1.image = [UIImage imageWithData:imageData1]; 

但問題是,我做的這個權利我30應用程序啓動後... 我想加載其中大約5個,而不是加載其他。因爲當我嘗試同時加載所有這些應用程序時,我的應用程序並未啓動所有這些應用程序... 有什麼方法可以加載其中的幾個,然後等待,然後再加載其他應用程序?

回答

1

至於加載它們,你可能應該顯示一個微調器,開始在後臺加載圖像,然後在圖像準備就緒後用圖像替換微調器。

- (void) viewDidLoad { 
    UIActivityIndicator *spinner = …; 
    [self.view addSubview:spinner]; 
    [self performSelectorInBackground:@selector(startLoadingImage) 
     withObject:nil]; 
    } 
- (void) startLoadingImage { 
    // You need an autorelease pool since you are running 
    // in a different thread now. 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    UIImage *image = [UIImage imageNamed:@"foo"]; 
    // All GUI updates have to be done from the main thread. 
    // We wait for the call to finish so that the pool won’t 
    // claim the image before imageDidFinishLoading: finishes. 
    [self performSelectorOnMainThread:@selector(imageDidFinishLoading:) 
     withObject:image waitUntilDone:YES]; 
    [pool drain]; 
} 

- (void) imageDidFinishLoading: (UIImage*) image { 
    // fade spinner out 
    // create UIImageView and fade in 
} 
+0

哦,它只是讓我困惑......讓我想想。 –