繁体   English   中英

使用GCD将图像异步加载到阵列

[英]Load images asyncroniously to the array using GCD

我尝试使用PanoramaGL框架显示全景图。 服务器端为多维数据集全景图的每一面返回几个平铺图像。 因此,我需要将该图像异步加载到数组中,此后,我需要从这些图像制作较大的侧面纹理-该任务第一部分的代码是:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

    int columnCount;
    int rowCount;

    NSString *side;

    if ([level  isEqual: @"3"]) {
        columnCount = 1;
        rowCount = 1;
    } else if ([level  isEqual: @"2"]) {
        columnCount = 2;
        rowCount = 2;

...这里我准备url字符串参数

    if (face == PLCubeFaceOrientationFront)
        side = @"F";
    else if (face == PLCubeFaceOrientationBack)
        side = @"B";

...这里我准备url字符串参数

    self.tileArray = [[NSMutableArray alloc] initWithCapacity:columnCount];

    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {

            NSString *tileUrl = [NSString stringWithFormat:@"%d_%d", columnIndex, rowIndex];

            NSMutableArray *tileUrlComponents = [NSMutableArray array];

            [tileUrlComponents addObject: panoramaData[@"id"]];
            [tileUrlComponents addObject: side];
            [tileUrlComponents addObject: level];
            [tileUrlComponents addObject: tileUrl];

            NSString *tileIdString = [tileUrlComponents componentsJoinedByString:@"/"];

            NSString *panoramaIdUrlString = [NSString stringWithFormat:@"http://SOMEURL=%@", tileIdString];

            NSURL *url = [NSURL URLWithString:panoramaIdUrlString];
            NSURLRequest *req = [NSURLRequest requestWithURL:url];

            [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *res, NSData *data, NSError *err) {
                UIImage *image = [UIImage imageWithData:data];
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSMutableArray *array = [self.tileArray objectAtIndex:columnIndex];
                    [array replaceObjectAtIndex:rowIndex withObject:image];
                    [self.tileArray addObject:array];
                });
            }];

            NSLog(@"The URL for the %@ tile is %@", tileUrl, tileIdString);
        }
    }
});

主要问题-如何理解我的数组已加载-现在可以使用其中的图像了。 第二个问题是不幸的是我现在得到了空数组。 有什么帮助吗?

我不确定您是否考虑使用操作。

这就是我会做的:

  1. 为每个请求创建NSOperation (确保在您的代码中该操作保持活动状态,直到完成加载)
  2. 将每个操作添加到NSOperationQueue (通过调用-operations您可以看到正在进行多少操作)
  3. 如果在后线程上实现NSOperationQueue ,则可以使用-waitUntilAllOperationsAreFinished (此方法将阻止当前线程,直到下载-waitUntilAllOperationsAreFinished所有图像为止),然后执行代码以显示图像。 您也可以使用-(void)addOperations:(NSArray *)ops waitUntilFinished:(BOOL)wait将所有NSOperation's添加到NSArray并添加到NSOperationQueue

你可以做这样的事情

dispatch_queue_t队列= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0ul); dispatch_async(queue,^ {

        dispatch_sync(dispatch_get_main_queue(), ^{

            NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
            UIImage *image = [[UIImage alloc] initWithData: data];


            [UIImageJPEGRepresentation(image, 100) writeToFile: imagePath atomically: YES];


        });
    });

您可以建立一个临时目录并在其中存储图像…,而在检索时,您可以仅使用简单方法as。

UIImage * image = [UIImage imageWithCOntentsOFFile:imagePath];。

它对我有用...尝试一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM