How to create an array of UIImages

前端 未结 2 1512
小鲜肉
小鲜肉 2021-01-23 08:26

I\'m storing an image from a Parse database like this:

PFFile *firstImageFile = self.product[@\"firstThumbnailFile\"];
[firstImageFile getDataInBackgroundWithBlo         


        
2条回答
  •  耶瑟儿~
    2021-01-23 09:23

    This is form of a common problem: how to do many asynch operations (without deeply nesting completion blocks) and know when they complete. The approach I use is to think of the parameters to the operations as a todo list, and build a method that handles the list recursively....

    - (void)loadPFFiles:(NSArray *)array filling:(NSMutableDictonary *)results completion:(void (^)(BOOL))completion {
        NSInteger count = array.count;
        // degenerate case is an empty array which means we're done
        if (!count) return completion(YES);
    
        // otherwise, do the first operation on the to do list, then do the remainder
        PFFile *file = array[0];
        NSArray *remainder = [array subarrayWithRange:NSMakeRange(0, count-1)];
    
        [file getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
            if (!error) {
                UIImage *image = [UIImage imageWithData:imageData];
                results[file.name] = image;
                [self loadPFFiles:remainder filling:results completion:completion];
            } else {
                completion(NO);
            }
        }];
    }
    

    Call it like this (guessing about your model a little bit):

    NSArray *pfFiles = @[ self.product[@"firstThumbnailFile"], self.product[@"secondThumbnailFile"] ];
    NSMutableDictionary *result = [@{} mutableCopy];
    
    [self loadPFFiles:pfFiles filling:result completion:^(BOOL success) {
        if (success) {
            // result will be an dictionary of the loaded images
            // indexed by the file names
        }
    }];
    

提交回复
热议问题