LBCache 是一个异步图像缓存框架,用于 iOS。
有 3 个选项可供使用
LBCacheImageOptionsDefault
- 默认选项将首先搜索缓存,如果找不到图像,则从网络下载。LBCacheImageOptionsReloadFromWeb
- 重新加载选项将始终从网络下载图像,即使它已经存在于缓存中。LBCacheImageOptionsLoadOnlyFromCache
- 缓存选项将只搜索缓存 __weak UIImageView *weakImgView = cell.imgView;
[cell.imgView setImageWithURLString: self.array[indexPath.row] placeholderImage: nil options: LBCacheImageOptionsDefault progressBlock:^(NSUInteger percent) {
NSLog(@"percent: %ld",percent);
} completionBlock:^(UIImage * image, NSError * error) {
dispatch_async(dispatch_get_main_queue(), ^{
weakImgView.image = image;
});
}];
这个类由 UIImageView 分类用于下载,但您也可以直接使用它。您可以使用 LBCacheManager 中的方法从磁盘或图像缓存的路径位置获取 UIImage 对象。
imagePathLocationForURLString:
- 一个包含图像在磁盘上保存的本地路径位置的字符串,如果未找到 URLString 的图像则为 nil。imageForURLString:
- 与 UIImageView+LBCategory 相同,直接在缓存(内存或磁盘)中搜索 UIImage,如果未找到则返回 nil。 __weak UIImageView *weakImgView = self.imgView;
[[LBCacheManager sharedInstance] downloadImageFromURLString: self.array[indexPath.row] options: LBCacheImageOptionsDefault progressBlock:^(NSUInteger percent) {
NSLog(@"percent: %ld",percent);
} completionBlock:^(UIImage * image, NSError * error) {
dispatch_async(dispatch_get_main_queue(), ^{
weakImgView.image = image;
});
}];
// you can also get the image from the disk using the urlString.
UIImage *image = [[LBCacheManager sharedInstance] imageForURLString: urlString];
// you can get the image path where it is cached.
NSString *imgPath = [[LBCacheManager sharedInstance] imagePathLocationForURLString:urlString];
您可以使用这个类分类从字符串获得哈希值。
MD5, SHA1 和 SHA256
可用的方法
lbHashMD5
- 创建一个 MD5 哈希lbHashSHA1
- 创建一个 SHA1 哈希lbHashSHA256
- 创建一个 SHA256 哈希lbHashWithType:
- 使用可用选项之一创建一个哈希 // you can use lbHashMD5, lbHashSHA1 or lbHashSHA256 method to get the specific hash from a string.
NSString *hashStr = [urlString lbHashSHA1];
// you can also use the lbHashWithType method and pass an option HashTypeMD5, HashTypeSHA1 or HashTypeSHA256
NSString *hashStr = [urlString lbHashWithType: HashTypeMD5];
此内容根据 MIT 许可证发布 https://github.com/lucianboboc/LBCache/blob/master/LICENSE.md
请享受!