[英]How to load data from cache using AFNetworking in iOS?
我正在使用AFNetworking庫來調用Web服務。 我的應用程序在集合視圖單元格和表視圖單元格中具有圖像視圖。 每次加載我的應用程序時,都會花費大量時間來加載圖像。 所以我想從緩存中加載圖像的方法。 我已經附加了部分代碼。
- (void)loadcategoryData
{
post = nil;
NSString *mainurl = [NSString stringWithFormat:@"some url"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager GET:mainurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
posts = (NSDictionary *)responseObject;
post = [NSMutableArray array];
for(NSDictionary *all in posts)
{
Categories *category = [Categories new];
category.title = [all objectForKey:@"catname"];
category.firsturl = [all objectForKey:@"url"];
[self.maincollectionView reloadData];
//call for images
imagespost = nil;
NSString *imageUrl = [NSString stringWithFormat:@"%@", category.firsturl];
AFHTTPRequestOperationManager *managerone = [AFHTTPRequestOperationManager manager];
[managerone GET:imageUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
imagesposts = (NSDictionary *)responseObject;
NSArray *resultone = [imagesposts objectForKey:@"posts"];
imagespost = [NSMutableArray array];
if ([resultone count]) {
NSDictionary *firstpost = resultone[0];
// Categories *newmogocat = [Categories new];
NSDictionary *thumbnail_images = [firstpost objectForKeyedSubscript:@"thumbnail_images"];
NSDictionary *thumbnail = [thumbnail_images objectForKey:@"thumbnail"];
category.imageOneUrl = [NSString stringWithFormat:@"%@",[thumbnail objectForKey:@"url"]];
// NSLog(@"%@", newmogocat.imageOneUrl);
// [imagespost addObject:newmogocat];
[post addObject:category];
[self.maincollectionView reloadData];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Something Wrong!" message:[NSString stringWithFormat:@"%@", error.localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[erroralert show];
}];
}
} failure:^(AFHTTPRequestOperation *operation, NSError * responseObject) {
}];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSMutableDictionary *mutableUserInfo = [[cachedResponse userInfo] mutableCopy];
NSMutableData *mutableData = [[cachedResponse data] mutableCopy];
NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowedInMemoryOnly;
return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:mutableData userInfo:mutableUserInfo storagePolicy:storagePolicy];
}
這是返回單元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellidentifier" forIndexPath:indexPath];
// cell.layer.borderWidth = 2.0f;
cell.layer.masksToBounds = NO;
cell.layer.cornerRadius = 5.0;
cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 1);
cell.layer.shadowRadius = 4.0;
cell.layer.shadowColor = [UIColor darkGrayColor].CGColor;
[cell addSubview:cell.maintitleLabel];
Categories *cat = [post objectAtIndex:indexPath.row];
[self.maincollectionView reloadInputViews];
cell.maintitleLabel.text = [NSString stringWithFormat:@" %@ ", cat.title];
[cell.maintitleLabel sizeToFit];
NSString *mainstring = [NSString stringWithFormat:@"%@", cat.imageOneUrl];
NSURL *url = [NSURL URLWithString:mainstring];
//
[cell.mainImageView setImageWithURL:url placeholderImage:nil];
//
return cell;
}
您需要的是一個單獨的類。 此類的每個對象都知道如何異步下載單個圖像,並將其緩存並通知與之關聯的圖像視圖該圖像已准備好顯示...
幸運的是AFNetworking已經為您編寫了此內容。 檢出其對UIImageView的擴展。 使用此擴展,您只需將圖像的URL傳遞到UIImageView(帶有setImageWithURL
),它就會為您完成所有艱苦的工作。
對於不使用AFNetworking的用戶,Apple提供了示例代碼: https : //developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.