如何创建一个UIImages数组 [英] How to create an array of UIImages

查看:93
本文介绍了如何创建一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆