IOS应用程序从Web加载数据

时间:2013-08-02 02:14:11

标签: ios web uiimageview load

我正在尝试从网上加载数据,只需几个简单的步骤:

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 个答案:

答案 0 :(得分:0)

至于加载它们,你应该显示一个微调器,开始在后台加载图像,然后在它准备好后用图像替换微调器。

- (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
}