ios - 我应该如何使用 AFNetworking 预加载所有图像并缓存它们?

标签 ios caching afnetworking

我需要在应用程序开始时预加载并缓存所有(近 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];

图像应该直接显示,因为它们都应该在缓存中。但他们加载得有些晚。我是否在某些代码中写错了?

最佳答案

我想你不会等待所有图像都被下载

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

 }]

;

关于ios - 我应该如何使用 AFNetworking 预加载所有图像并缓存它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12882639/

相关文章:

ios - 使用 AFNetworking 上传大文件?

iphone - iOS 上的延迟执行技术

objective-c - 具有(复杂)关系和核心数据/RAILS 的 Restkit

ios - Unity iOS 屏幕在启动时变黑几秒钟

caching - SW-Architecture - 服务层和持久层之间的缓存

caching - 分布式缓存 - 基本概念

.htaccess - 在.htaccess文件中启用SVG文件的压缩和缓存

ios - 如何使用 AFNetworking 管理 session ?

ios - 我可以获取纬度和经度,但无法访问 SWIFT 中的 GPS 高度信息

ios - 单击纯 html 链接时,Apple 如何检测是否安装了 iphone 应用程序?