AFNetworking是自动加载图像还是我们必须手动加载?

时间:2015-08-02 08:38:45

标签: ios afnetworking afnetworking-2

我使用AFNetworking从JSON Feed加载图片。

在这里,用户首次打开应用时,图像会从互联网上加载。没关系。

但是当用户返回并从另一个视图再次使用时,图像应该从缓存加载,而不是从互联网加载。

我该怎么做?

- (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;
}

1 个答案:

答案 0 :(得分:2)

tl; dr

使用-setImageWithURLRequest:placeholderImage:success:failure:

  NSURLRequest *imageRequest =
      [NSURLRequest requestWithURL:[NSURL URLWithString:imgurl]
                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                   timeoutInterval:60];

  [cell.detailImageView setImageWithURLRequest:imageRequest
                              placeholderImage:nil
                                       success:nil
                                       failure:nil];

图像缓存+ AFNetworking

导入 UIImageView + AFNetworking 标题和中提琴! UIImageView类现在有几种方法可以下载图像,也可以缓存它们!

内存中缓存: 您可以使用以下方法进行简单的内存缓存。

如果图像存在,将从内存缓存中检索图像,否则将从URL下载并存储在内存缓存中。

示例:

[imageView setImageWithURL:[NSURL URLWithString:imageURL]];

//here, placeholder image is set immediately.
[imageView setImageWithURL:[NSURL URLWithString:imageURL]
          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

磁盘缓存: 如果您需要将图片缓存的时间超过用户的会话,则可以使用以下方法。 (提示:检查NSURLRequestCachePolicy班级中的NSURLRequest

注意:如果指定了成功块,则块的责任是在返回之前设置图像视图的图像。如果未指定成功阻止,则应用使用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];