如何创建一组 UIImages [英] How to create an array of UIImages
问题描述
我正在像这样从 Parse 数据库存储图像:
I'm storing an image from a Parse database like this:
PFFile *firstImageFile = self.product[@"firstThumbnailFile"];
[firstImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
self.firstImage = [UIImage imageWithData:imageData];
}
}];
我想将图像保存为数组以在滚动视图中显示它们.
I want to save the images as an array to display them inside a scrollview.
如果我做这样的事情它会起作用:
It works if I do something like this:
self.galleryImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"s2.jpg"], [UIImage imageNamed:@"s1.jpg"], nil];
但是如果我尝试使用 UIImage 本身,则不会出现图像.
But if I try to use the UIImage itself, no image appears.
self.galleryImages = [NSArray arrayWithObjects: self.firstImage, self.secondImage, nil];
有什么帮助吗?谢谢.
推荐答案
这是一个常见问题的形式:如何执行许多异步操作(无需深度嵌套完成块)并知道它们何时完成.我使用的方法是将操作的参数视为待办事项列表,并构建一个递归处理列表的方法......
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
}
}];
这篇关于如何创建一组 UIImages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!