简体   繁体   中英

Load images asyncroniously to the array using GCD

I try to use panoramaGL framework to show panorama. The server-side returns several tile images for every side of cube panorama. So I need to load this images asyncroniously into the array and after this I need to make a big side texture from this images - the code for the first part of this task is:

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;

... here I prepare the url string parameters

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

... here I prepare the url string parameters

    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);
        }
    }
});

The main question - how can I understand, that my array is loaded - and I can now work with the images in it. The second question is that I get the empty array now unfortunately. Any help?

I am not sure you considered using operations.

This is how I would do:

  1. Create NSOperation for each request (make sure in your code the operation stays alive until it has finished loading)
  2. Add each operation to NSOperationQueue . (By calling -operations you can see how many operations are in progress)
  3. If you implement NSOperationQueue on back thread you can use -waitUntilAllOperationsAreFinished (this method blocks current thread until all your images have been downloaded) and then execute your code to display images. You can alternatively add all NSOperation's to NSArray and add to NSOperationQueue by using -(void)addOperations:(NSArray *)ops waitUntilFinished:(BOOL)wait

You can do something like this

dispatch_queue_t queue = 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];


        });
    });

where imagePath ia a path in your applications directory..You can make a temporary directory and store the images there…and while retrieving you can just use the simple method as..

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

It worked for me…just give it a try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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