【问题标题】:Does AFNetworking cache images load automatically or do we have to do it manually?AFNetworking 缓存图像是自动加载还是我们必须手动加载?
【发布时间】:2015-08-02 08:38:45
【问题描述】:

我正在使用 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;
}

【问题讨论】:

    标签: ios afnetworking afnetworking-2


    【解决方案1】:

    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"]];
    

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-16
      • 1970-01-01
      • 2013-06-01
      • 2013-02-12
      • 2011-04-12
      • 2020-03-23
      • 2014-10-02
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多