1

したがって、このコードは基本的にサーバーからデータを取得し、画像を作成してからビューに追加します。

ただし、一度に1つずつビューを更新したいと思います。

現在、すべての画像が読み込まれた後、ビューは一度にすべての画像で更新されます。

新しい画像がアップロードされるたびにビューを更新したい - これは addImage メソッドで発生するはずですが、代わりに、すべての画像が読み込まれるのを待っています。

この問題を解決するためにdispatch_asyncを使用しようとしましたが、うまくいきませんでした。

では、一度に 1 つずつ画像を読み込むにはどうすればよいでしょうか。

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

は同期操作であるため、各画像を 1 つずつ待機する必要があります。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 に答える