我应该如何预加载所有图像并使用AFNetworking缓存它们? [英] How should I pre-load all images and cache them using AFNetworking?

查看:96
本文介绍了我应该如何预加载所有图像并使用AFNetworking缓存它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在应用程序开始时预加载并缓存所有(近80个)图像,同时向用户显示Please Wait。我做的是:

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?

推荐答案

我想你不要等到所有图片都会被下载

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

 }]

;

这篇关于我应该如何预加载所有图像并使用AFNetworking缓存它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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