1

所以这段代码基本上是从服务器获取数据,创建图像,然后将其添加到视图中。

但是,我希望它一次更新一个视图。

现在,视图在所有图像都加载后立即更新。

我想在每次上传新图像时更新视图 - 这应该发生在 addImage 方法中,但它只是在等待所有图像加载。

我尝试使用 dispatch_async 来解决这个问题,但它不起作用。

那么如何一次加载一张图片呢?

    AFJSONRequestOperation *operation =
        [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
       //some code not displayed here that parses the JSON
       for (NSDictionary *image in images){
           if ([[image objectForKey:@"ContentType"] isEqualToString:@"image/png"]){
               NSString *imgLink = [[image objectForKey:@"MediaUrl"] JSONStringWithOptions:JKSerializeOptionPretty includeQuotes:NO error:nil];
               NSURL *url = [NSURL URLWithString:path];
               NSData *data = [NSData dataWithContentsOfURL:url];
               UIImage *img = [[UIImage alloc] initWithData:data cache:NO]
               dispatch_async(dispatch_get_main_queue(), ^{
                   [self addImage:img];
                   //This method adds the image to the view
                });
           }
       }
   }

更新:这不是表格视图,图像作为子视图添加到 UIView

4

2 回答 2

0
[NSData dataWithContentsOfURL:url];

是同步操作,所以你必须一张一张地等待每张图片,基于url的图片的适当延迟加载实现是最好的解决方案,同时你可以尝试做这样的事情

AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                                    //some code not displayed here that parses the JSON
                                                    for (NSDictionary *image in images){
                                                        if ([[image objectForKey:@"ContentType"] isEqualToString:@"image/png"]){
                                                            NSString *imgLink = [[image objectForKey:@"MediaUrl"] JSONStringWithOptions:JKSerializeOptionPretty includeQuotes:NO error:nil];
                                                            NSURL *url = [NSURL URLWithString:path];

                                        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                                                NSData *data = [NSData dataWithContentsOfURL:url];
                                                                UIImage *img = [[UIImage alloc] initWithData:data cache:NO]
                                                                dispatch_async(dispatch_get_main_queue(), ^{
                                                                    [self addImage:img];
                                                                    //This method adds the image to the view
                                                                });
                                                            })

                                                        }
                                                    }
                                                }

如果您想使用此解决方案,建议您创建自己的并发队列而不是使用 global_queue

于 2013-04-05T16:02:50.083 回答
0

我相信您正在寻找一种延迟加载的形式。试试这个例子:Apple 延迟加载示例

于 2013-04-05T15:31:31.253 回答