ThumbnailService - 是一个帮助您异步生成预览图片的服务。
例如,您有一个大图片,20+ 兆字节。您想显示该图片的缩略图。
TSSourceImage *imageSource = [[TSSourceImage alloc] initWithImagePath:hugeImagePath];
TSRequest *thumbnailRequest = [TSRequest new];
thumbnailRequest.source = imageSource;
thumbnailRequest.size = self.imageView.bounds.size;
[thumbnailRequest setThumbnailCompletion:^(UIImage *result, NSError *error) {
weakSelf.imageView.image = result;
}];
[thumbnailService enqueueRequest:thumbnailRequest]
就是这样。将返回指定大小的缩略图到完成块。
让我们来做一个更复杂的例子。您想先显示小的(模糊的)缩略图,然后显示完整的缩略图。
很简单!TSRequestGroupSequence 是您的朋友!
TSSourceImage *imageSource = [[TSSourceImage alloc] initWithImagePath:imagePath];
CGSize smallThumbnailSize = CGSizeApplyAffineTransform(self.imageView.bounds.size, CGAffineTransformMakeScale(0.5, 0.5));
TSRequest *smallThumbRequest = [TSRequest new];
smallThumbRequest.source = imageSource;
smallThumbRequest.size = smallThumbnailSize;
smallThumbRequest.queuePriority = NSOperationQueuePriorityVeryHigh;
[smallThumbRequest setThumbnailCompletion:^(UIImage *result, NSError *error) {
weakSelf.imageView.image = result;
}];
TSRequest *bigThumbRequest = [TSRequest new];
bigThumbRequest.source = imageSource;
bigThumbRequest.size = self.imageView.bounds.size;
bigThumbRequest.queuePriority = NSOperationQueuePriorityHigh;
[bigThumbRequest setThumbnailCompletion:^(UIImage *result, NSError *error) {
weakSelf.imageView.image = result;
}];
TSRequestGroupSequence *group = [TSRequestGroupSequence new];
[group addRequest:smallThumbRequest];
[group addRequest:bigThumbRequest];
[thumbnailService enqueueRequestGroup:group];
在这个例子中,thumbnailService 按顺序执行 smallThumbRequest 然后执行 bigThumbRequest。
有时您想同步执行请求。例如,如果图像已在磁盘上缓存,则将其加载到主线程上是廉价的。
if ([thumbnailService hasDiskCacheForRequest:request]) {
[thumbnailService executeRequest:request];
} else {
[thumbnailService enqueueRequest:request];
}
那么,关于主动缓存呢?是的,它就是为此而设计的!您有一个 PDF 文档,并想在阅读器中预缓存所有页面。这也很简单
- (void) precachePagesForDocument:(CGPDFDocumentRef)document withName:(NSString *)documentName
{
NSUInteger pagesCount = CGPDFDocumentGetNumberOfPages(document);
for (int i = 1; i < pagesCount; i++) {
CGPDFPageRef page = CGPDFDocumentGetPage(document, i);
TSRequest *request = [TSRequest new];
request.source = [[TSSourcePDFPage alloc] initWithPdfPage:page documentName:documentName];
request.size = kThumbnailSize;
request.queuePriority = NSOperationQueuePriorityVeryLow;
request.shouldCacheInMemory = NO;
[request setThumbnailCompletion:^(UIImage *result, NSError *error) { /* You have to pass it empty, not nil */ }];
if (![thumbnailService hasDiskCacheForRequest:request]) {
[thumbnailService enqueueRequest:request];
}
}
}
同时您还可以在阅读器中请求缩略图
- (void) loadThumbnailAtIndex:(NSInteger)index intoImageView:(UIImageView *)imageView
{
CGPDFPageRef page = CGPDFDocumentGetPage(document, index);
TSRequest *request = [TSRequest new];
request.source = [[TSSourcePDFPage alloc] initWithPdfPage:page documentName:documentName];
request.size = kThumbnailSize;
request.queuePriority = TSRequestQueuePriorityVeryHigh;
[thumbnailService enqueueRequest:request];
}
ThumbnailService 不会执行两次请求,它将它们组合并按最高请求优先级执行单个操作。
当您实现具有滚动能力的阅读器时,不要忘记取消您不需要的请求(对不可见项目的请求)。
UIImageView *reusableImageView = ...
TSRequest *oldRequest = reusableImageView.currentThumbnailRequest;
[oldRequest cancel];
TSRequest *newRequest = ...
[thumbnailService enqueueRequest:newRequest];
reusableImageView.currentThumbnailRequest = newRequest;
这种方法将确保可见项将以最短的可能时间由缩略图填充。
首选方式是使用 Cocoapods
pod 'ThumbnailService'
但您也可以导入源或用作静态库
您可以通过子类化 TSSource 或其他源来添加自己的缩略图源。有关更多信息,请参阅 ThumbnailServiceDemo。
克隆和检查示例