0

我正在尝试通过几个简单的步骤从网络加载数据:

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 个,而不是加载其他的。因为当我尝试同时加载所有这些时,我的应用程序并没有启动所有加载...有什么办法可以加载前几个,然后等待,然后再加载其他?

4

1 回答 1

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
}
于 2013-08-02T05:09:56.103 回答