0

ユーザーに「お待ちください」と表示しながら、アプリケーションの開始時にすべての (ほぼ 80 の) 画像をプリロードしてキャッシュする必要があります。私がしたことは次のとおりです。

NSMutableArray *operations = [[NSMutableArray alloc] init];
   for(Category *c in shop.menu.categories){
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:c.imagePath] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
        AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil cacheName:@"nscache"
                                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                                                                                  }
                                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                                                                           }];
    [operations addObject:operation];


    for(Item *i in c.items){
        NSURLRequest *request2 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:i.imagePath] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
        AFImageRequestOperation *operation2 = [AFImageRequestOperation imageRequestOperationWithRequest:request2 imageProcessingBlock:nil cacheName:@"nscache"
                                                                                                success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                                                                                }
                                                                                                failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                                                                                }];
        [operations addObject:operation2];

    }
}

[[APIClient sharedClient] enqueueBatchOfHTTPRequestOperations:operations
                                                progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations)
 {
     float percentDone = 100 * ((float)((int)numberOfCompletedOperations) / (float)((int)totalNumberOfOperations));
     //appDelegateHUD.progress = percentDone;
     NSLog([NSString stringWithFormat:@"%f", percentDone]);
 }
                                              completionBlock:^(NSArray *operations)
 {
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

     [appDelegate hideHUDWithDelay:0];

 }];

このコード ブロックでは、一部の画像は正常にキャッシュされますが、他の画像はキャッシュされません。私はこのコードブロックを使用しています:

        AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest
                                                                                  imageProcessingBlock:nil
                                                                                             cacheName:@"nscache"
                                                                                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                                                                                   [UIView beginAnimations:@"ToggleViews" context:nil];
                                                                                                   [UIView setAnimationDuration:1.0];
                                                                                                   imageview.image = image;
                                                                                                   [UIView commitAnimations];
                                                                                               }
                                                                                               failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ NSLog([error description]); }
                                              ];
        [operation start];

画像はすべてキャッシュにある必要があるため、直接表示する必要があります。しかし、それらは少し遅れてロードされます。コードの一部が間違っていますか?

4

1 に答える 1

1

すべての画像がダウンロードされるまで待たないでください

[[APIClient sharedClient] enqueueBatchOfHTTPRequestOperations:operations
                                                progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations)
 {
     float percentDone = 100 * ((float)((int)numberOfCompletedOperations) / (float)((int)totalNumberOfOperations));
     //appDelegateHUD.progress = percentDone;
     NSLog([NSString stringWithFormat:@"%f", percentDone]);
 }
                                              completionBlock:^(NSArray *operations)
 {
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

     [appDelegate hideHUDWithDelay:0];
// here you just hide HUD, but here you should start load images to UI

 }]

;

于 2012-10-14T13:52:42.067 に答える