uiimageView afnetworking和保存图像



我需要将图像设置为我的ImageViews。而且有很多图像(我认为它将接近200MB)。我需要保存全部,以便在没有Internet连接的情况下在本地使用应用程序。使用类别UIImageView+AFNetworking非常容易,但是我不明白它如何保存和何处?

因此,在这里订阅方法,您可以看到它使用NSURLCacheStorageAllowed的缓存策略。因此,保存在磁盘上的高速缓存文件夹中的图像,对吗?没关系,但是此存储的限制是什么?我需要实现下一个代码:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  //another code...
  NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 
                                                   diskCapacity:200 * 1024 * 1024 
                                                       diskPath:nil];
  [NSURLCache setSharedURLCache:URLCache];
  return YES;
}

因此,NSURLCacheStorageAllowedNSCachedURLResponse一样返回storagePolicy。因此,我知道我不得实现上面写的代码。

如果我使用UIImageView+AFNetworking类别,我所有的图像都会保存在当地的缓存存储中?

uiimageView 您发现的afnetworking依靠基础URL加载系统来缓存数据。diskCapacity将确定应用程序一次想要多少存储空间。这也将依靠服务器在递给您图像时指定适当的Cache-Control标头 - 在某些情况下,如果缓存时间太短,则NSURLCACHE根本不会存储它。

为了更多地控制客户端的磁盘缓存,您可以查看SDWebimage。

sdwebimage具有异步图像下载,对缓存有很大的控制 - 哪些图像被缓存,在磁盘或内存中的时间,等等。 afnetworking可能不会为您提供所需的控制,您应该探索此选择。

我做过相同的想法,当我需要在tableview中显示图像时,首先我检查图像是否在本地可用,然后下载图像并像此图像一样保存

if (userBasicInfo.userImage == nil) {
            __weak LGMessageBoxCell *weakCell = cell;
            [cell.userImage setImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:userBasicInfo.imageUrl]]
                                  placeholderImage:[UIImage imageNamed:@"facebook-no-user.png"]
                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                               weakCell.userImage.image = image;
                                               [weakCell setNeedsLayout];
                                               [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
                                                   UserBasicInfo* userBasicInfo = [[UserBasicInfo findByAttribute:@"userId" withValue:@(chatUser) inContext:localContext] objectAtIndex:0];
                                                   userBasicInfo.userImage = UIImagePNGRepresentation(image);
                                               } completion:^(BOOL success, NSError *error) {
                                                   NSLog(@"%@",[error localizedDescription]);
                                               }];
                                           }
                                           failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                           }];
        } else {
            cell.userImage.image = [UIImage imageWithData:userBasicInfo.userImage];
        }

最新更新