Ios 我应该如何预加载所有图像并使用AFNetworking缓存它们?
我需要在应用程序开始时预加载和缓存所有(近80个)图像,同时向用户显示“请稍候”。我所做的是:Ios 我应该如何预加载所有图像并使用AFNetworking缓存它们?,ios,caching,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.imagePat
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
}]
) 我不需要将图像加载到该视图中的UI。但在下一个视图中,所有图像都应该来自缓存。如果我使用NSURLRequestReturnCacheDataDontLoad,则无法加载某些图像。好的,在另一个视图中,您是否等待加载它们?完成块是否告诉我们操作已完成?这是错的吗?完成块应该告诉它,但我只看到隐藏的HUD。所以我想你不会等待所有的图像加载我不想在那个地方加载图像到UI。唯一我想要的是所有的图像都预加载到磁盘,并准备好供进一步使用。