How to create an array of UIImages

前端 未结 2 1484
小鲜肉
小鲜肉 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:30

    I would guess (based on your comments about nil above) that your code looks a little like this:

    PFFile *firstImageFile = self.product[@"firstThumbnailFile"];
    [firstImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
            self.firstImage = [UIImage imageWithData:imageData];
        }
    }];
    
    self.galleryImages = [NSArray arrayWithObjects: self.firstImage, self.secondImage, nil];
    

    If this is the case, move the array initialization inside the completion block like so:

    PFFile *firstImageFile = self.product[@"firstThumbnailFile"];
    [firstImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
            self.firstImage = [UIImage imageWithData:imageData];
    
            dispatch_async(dispatch_get_main_queue(), ^{
    
                    self.galleryImages = [NSArray arrayWithObjects: self.firstImage, self.secondImage, nil];
                });
        }
    }];
    

    What happens in the first (your) case is that the array initialization statement runs before the completion block, so when 'first image' is actually set it is too late as the array has already been initialized.

提交回复
热议问题