我正在使用AFNetworking从JSON提要加载图像。
在这里,当用户第一次打开应用程序时,图像从互联网加载。它很好。
但是当用户从另一个视图返回时,在使用应用程序时,图像应该从缓存加载,而不是从互联网加载。
我该怎么做呢?
- (void)loadDetailData
{
detailPost = nil;
NSString *detailUrl = [NSString stringWithFormat:@"%@", self.Details.firsturl];
AFHTTPRequestOperationManager *detailManager = [AFHTTPRequestOperationManager manager];
[detailManager GET:detailUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
detailPosts = (NSDictionary *)responseObject;
detailPost = [NSMutableArray array];
NSArray *result = [detailPosts objectForKey:@"posts"];
for (NSDictionary *all in result)
{
Categories *newCategory = [Categories new];
NSDictionary *thumbnail_images = [all objectForKey:@"thumbnail_images"];
NSDictionary *mediumImages = [thumbnail_images objectForKey:@"medium"];
newCategory.detailimageUrl = [mediumImages objectForKey:@"url"];
newCategory.title = [all objectForKey:@"title"];
newCategory.mogowebUrl = [all objectForKey:@"url"];
// NSLog(@"%@", newCategory.title);
[detailPost addObject:newCategory];
[self.maintableView reloadData];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [detailPost count];;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
Categories *details = [detailPost objectAtIndex:indexPath.row];
cell.detailTitle.text = details.title;
NSString *imageurl = [NSString stringWithFormat:@"%@", details.detailimageUrl];
NSURL *imgurl = [NSURL URLWithString:imageurl];
[cell.detailImageView setImageWithURL:imgurl placeholderImage:nil];
// [cell addSubview:cell.subView];
cell.layer.masksToBounds = NO;
cell.layer.cornerRadius = 5;
cell.layer.borderColor = [UIColor blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 1);
cell.layer.shadowRadius = 2.0;
cell.layer.shadowColor = [UIColor lightGrayColor].CGColor;
return cell;
}
tl;
使用-setImageWithURLRequest: placeholderImage:成功失败:
NSURLRequest *imageRequest =
[NSURLRequest requestWithURL:[NSURL URLWithString:imgurl]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:60];
[cell.detailImageView setImageWithURLRequest:imageRequest
placeholderImage:nil
success:nil
failure:nil];
Image Cache + AFNetworking
导入UIImageView+AFNetworking头文件和viola!UIImageView
类现在有几个方法来下载图像,也可以缓存它们!
内存缓存:您可以使用以下方法进行简单的内存缓存:
- - setImageWithURL:
- - setImageWithURL: placeholderImage:
图像将从内存缓存中检索,如果图像存在,则从URL下载并存储在内存缓存中。
的例子:
[imageView setImageWithURL:[NSURL URLWithString:imageURL]];
//here, placeholder image is set immediately.
[imageView setImageWithURL:[NSURL URLWithString:imageURL]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
磁盘缓存:如果需要缓存映像的时间超过用户的会话,可以使用以下方法。(提示:检查NSURLRequest
类中的NSURLRequestCachePolicy
)
- -setImageWithURLRequest: placeholderImage:成功:失败:
注意:如果指定了一个成功块,则该块负责在返回之前设置图像视图的图像。如果没有指定成功块,则应用默认行为设置带有self.image = image
的映像。
的例子:
NSURLRequest *imageRequest =
[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:60];
[imageView setImageWithURLRequest:imageRequest
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
success:nil
failure:nil];