【问题标题】:How should I pre-load all images and cache them using AFNetworking?我应该如何使用 AFNetworking 预加载所有图像并缓存它们?
【发布时间】:2012-10-14 21:52:42
【问题描述】:

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

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

【问题讨论】:

    标签: ios caching afnetworking


    【解决方案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
    
     }]
    

    ;

    【讨论】:

    • 我不需要在该视图中将图像加载到 UI。但在下一个视图中,所有图像都应该来自缓存。而如果我使用NSURLRequestReturnCacheDataDontLoad,有些图片无法加载。
    • 好的,在另一个视图中,但是你会等待它们被加载吗?
    • 完成块不是告诉我们操作完成了吗?错了吗?
    • 完成块应该告诉它,但我只看到隐藏 HUD。所以我想你不要等待加载所有图像
    • 我不想在那个地方将图像加载到 UI。只有我想要的是所有图像都预先加载到磁盘中,并准备好进一步使用。
    猜你喜欢
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2018-10-05
    • 1970-01-01
    • 2021-10-14
    • 2018-07-18
    相关资源
    最近更新 更多