How should I pre-load all images and cache them us

2019-04-17 13:31发布

I need to pre-load and cache all (Nearly 80) images in the beginning of the application, while showing "Please Wait" to the user. What I did is:

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

 }];

With this code block, some of the images are successfully cached, but others are not. I am using this code block:

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

Images should be displayed directly because they all should be in the cache. But they are loaded some late. Am I wrong in some piece of code?

1条回答
成全新的幸福
2楼-- · 2019-04-17 13:56

I think you dont't wait while all images will be downloaded

[[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

 }]

;

查看更多
登录 后发表回答