如何创build一个UIImage数组

我正在从一个Parse数据库像这样存储一个图像:

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

我想将图像保存为一个数组,以在滚动视图中显示它们。

它工作,如果我做这样的事情:

 self.galleryImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"s2.jpg"], [UIImage imageNamed:@"s1.jpg"], nil]; 

但是如果我尝试使用UIImage本身,则不会显示图像。

 self.galleryImages = [NSArray arrayWithObjects: self.firstImage, self.secondImage, nil]; 

任何帮助? 谢谢。

这是一个常见问题的forms:如何做很多asynchronous操作(没有深度嵌套完成块)并知道它们何时完成。 我使用的方法是将操作的参数视为待办事项列表,并构buildrecursion处理列表的方法….

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

这样称呼(猜测你的模型有点):

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

我猜(根据你对上面nil的评论)你的代码看起来有点像这样:

 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]; 

如果是这种情况,请在完成块内移动数组初始化,如下所示:

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

第一个(你)的情况是,数组初始化语句在完成块之前运行,所以当第一个图像被真正设置时,已经太迟了,因为数组已经被初始化了。