GDRSImageCache 是为 iOS 设计的简约缓存和图像调整大小库。给定一个 URL,它在后台线程中检索图像并在内存中缓存它。GDRSImageCache 可以提供一个过滤块,用于在缓存之前调整从 URL 获取的图像的大小。
从 GDRSImageCache
目录拖动以下四个文件到 XCode 的项目导航器中
同时确保它们已添加到您的目标中。
GDRSImageCache 的典型使用方式如下
UIImageView *anImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
// create the cache
GDRSImageCache *cache = [[GDRSImageCache alloc] initWithCachedImageFilter:^UIImage *(UIImage *sourceImage) {
// resize the image to the image view size and round the image corners;
// this is called by the cache on a background thread
return [sourceImage gdrs_resizedImageToAspectFitSize:anImageView.bounds.size cornerRadius:10];
}];
// set the default image, which will be returned from
// fetchImageWithURL:completionHandler: if an image coresponding to the
// requested url is not cached yet.
cache.defaultImage = [UIImage imageNamed:<#place holder image name#>];
NSURL *imageUrl = <#an url to a image#>;
// fetch an image; the call returns imidiatly and the callback handler
// is called when the image is fetched over the network
anImageView.image = [cache fetchImageWithURL:imageUrl completionHandler:^(UIImage *image, NSError *error) {
anImageView.image = image;
}];
要自己使用 GDRSImageCache 类,您需要 #import <GDRSImageCache/GDRSImageCache.h>
。对于 UIImage
的调整大小方法(例如 gdrs_resizedImageToAspectFitSize:
),您需要 #import <GDRSImageCache/UIImage+GDRSResizing.h>
。