Inherits from NSObject
Conforms to PSPDFRenderDelegate
Declared in PSPDFCache.h

Overview

This singleton manages both memory and disk cache, and adds new render requests to PSPDFRenderQueue. Most settings are device dependant.

Tasks

Other Methods

Access cache

  • – cacheStatusForImageFromDocument:andPage:withSize:options:

    Get the cache status of a rendered image. options will ignore all entires except PSPDFCacheOptionSize*.

  • – imageFromDocument:andPage:withSize:options:

    Get the image for a certain document page. Will first check the memory cache, then the disk cache and lastly queues a request to render. Returns the image instantly if the memory cache was filled, else will queue and call the delegate. If requireExactSize is set, images will either be downscaled or dynamically rendered. (There’s no point in upscaling)

Store into cache

  • – saveImage:fromDocument:andPage:withReceipt:

    Caches the image in memory and disk for later re-use. PSPDFCache will decide at runtime if the image is worth saving into memory or just disk. (And disk will only be hit if the image is different)

Document preprocessing

Cache invalidation

Access internal caches

  •   memoryCache

    Access the memory cache. Allows deeper customization of the amount of memory used.

    property
  •   diskCache

    Access the disk cache. Allows deeper customization of the amount of disk space used.

    property

Settings

  •   cacheDirectory

    Cache files are saved in a subdirectory of NSCachesDirectory. Defaults to “PSPDFKit/Pages”.

    property
  •   diskCacheStrategy

    Defines the global disk cache strategy. Defaults to PSPDFDiskCacheStrategyEverything. If PSPDFDocument also defines a strategy, that one is priorized.

    property
  •   thumbnailSize

    The size of the thumbnail images used in the grid view and those shown before the full-size versions are rendered. Defaults to (170, 220) on iPad and (85, 110) on iPhone.

    property
  •   tinySize

    The size of the images used in the scrobble bar. Defaults to CGSizeMake(50, 65).

    property

Starting/Stopping

  • – pauseCachingForService:

    Will pause queued cache requests on the render queue. For service use the class object that requests the pause.

  • – resumeCachingForService:

    Will resume queued cache requests on the render queue. For service use the class object that requested the pause.

Delegate

Disk Cache Settings

  •   useJPGFormat

    JPG is almost always faster, and uses less memory (<50% of a PNG, usually). Defaults to YES. If you have very text-like pages, you might want to set this to NO.

    property
  •   JPGFormatCompression

    Compression strength for JPG. (PNG is lossless) The higher the compression, the larger the files and the slower is decompression. Defaults to 0.9. This will load the pdf and remove any jpg artifacts.

    property
  •   allowImageResize

    Will allow image resizing instead of re-rendering to generate thumbnails. This is conditionally enabled for older devices. This is a tradeoff between performance or quality.

    property

Encryption/Decryption Handlers

  •   decryptFromPathBlock

    Decrypt data from the path. PSPDFKit Annotate feature. If set to nil, the default implementation will be used.

    property
  •   encryptDataBlock

    Encrypt mutable data. PSPDFKit Annotate feature. If set to nil, the default implementation will be used.

    property

Properties

JPGFormatCompression

Compression strength for JPG. (PNG is lossless) The higher the compression, the larger the files and the slower is decompression. Defaults to 0.9. This will load the pdf and remove any jpg artifacts.

@property (nonatomic, assign) CGFloat JPGFormatCompression

Declared In

PSPDFCache.h

allowImageResize

Will allow image resizing instead of re-rendering to generate thumbnails. This is conditionally enabled for older devices. This is a tradeoff between performance or quality.

@property (nonatomic, assign) BOOL allowImageResize

Discussion

Note: You will need to clear the cache after changing this to see a difference.

Declared In

PSPDFCache.h

cacheDirectory

Cache files are saved in a subdirectory of NSCachesDirectory. Defaults to “PSPDFKit/Pages”.

@property (nonatomic, copy) NSString *cacheDirectory

Discussion

Note: The cache directory is not backed up by iCloud and will be purged when memory is low.

Warning: Set this early during class initialization. Will clear the current chache before changing.

Declared In

PSPDFCache.h

decryptFromPathBlock

Decrypt data from the path. PSPDFKit Annotate feature. If set to nil, the default implementation will be used.

@property (atomic, copy) NSData *^ ) ( PSPDFDocument *document , NSString *path ) decryptFromPathBlock

Declared In

PSPDFCache.h

diskCache

Access the disk cache. Allows deeper customization of the amount of disk space used.

@property (nonatomic, strong, readonly) PSPDFDiskCache *diskCache

Declared In

PSPDFCache.h

diskCacheStrategy

Defines the global disk cache strategy. Defaults to PSPDFDiskCacheStrategyEverything. If PSPDFDocument also defines a strategy, that one is priorized.

@property (nonatomic, assign) PSPDFDiskCacheStrategy diskCacheStrategy

Declared In

PSPDFCache.h

encryptDataBlock

Encrypt mutable data. PSPDFKit Annotate feature. If set to nil, the default implementation will be used.

@property (atomic, copy) void ( ^ ) ( PSPDFDocument *document , NSMutableData *data ) encryptDataBlock

Declared In

PSPDFCache.h

memoryCache

Access the memory cache. Allows deeper customization of the amount of memory used.

@property (nonatomic, strong, readonly) PSPDFMemoryCache *memoryCache

Declared In

PSPDFCache.h

thumbnailSize

The size of the thumbnail images used in the grid view and those shown before the full-size versions are rendered. Defaults to (170, 220) on iPad and (85, 110) on iPhone.

@property (nonatomic, assign) CGSize thumbnailSize

Declared In

PSPDFCache.h

tinySize

The size of the images used in the scrobble bar. Defaults to CGSizeMake(50, 65).

@property (nonatomic, assign) CGSize tinySize

Declared In

PSPDFCache.h

useJPGFormat

JPG is almost always faster, and uses less memory (<50% of a PNG, usually). Defaults to YES. If you have very text-like pages, you might want to set this to NO.

@property (nonatomic, assign) BOOL useJPGFormat

Declared In

PSPDFCache.h

Class Methods

sharedCache

The cache object is a singleton.

+ (instancetype)sharedCache

Declared In

PSPDFCache.h

Instance Methods

addDelegate:

Register a delegate to be notifiec of new cache load events.

- (void)addDelegate:(id<PSPDFCacheDelegate>)aDelegate

Declared In

PSPDFCache.h

cacheDocument:startAtPage:sizes:diskCacheStrategy:

Starts caching the document. setting diskCacheStrategy to PSPDFDiskCacheStrategyEverything will pre-cache the whole document, and PSPDFDiskCacheStrategyNearPages will render a few pages around page.

- (void)cacheDocument:(PSPDFDocument *)document startAtPage:(NSUInteger)page sizes:(NSArray *)sizes diskCacheStrategy:(PSPDFDiskCacheStrategy)diskCacheStrategy

Declared In

PSPDFCache.h

cacheStatusForImageFromDocument:andPage:withSize:options:

Get the cache status of a rendered image. options will ignore all entires except PSPDFCacheOptionSize*.

- (PSPDFCacheStatus)cacheStatusForImageFromDocument:(PSPDFDocument *)document andPage:(NSUInteger)page withSize:(CGSize)size options:(PSPDFCacheOptions)options

Declared In

PSPDFCache.h

cancelRequestForImageFromDocument:andPage:withSize:

Cancels any open image request (disk load/render).

- (void)cancelRequestForImageFromDocument:(PSPDFDocument *)document andPage:(NSUInteger)page withSize:(CGSize)size

Declared In

PSPDFCache.h

clearCache

Clears the disk and memory cache.

- (void)clearCache

Declared In

PSPDFCache.h

imageFromDocument:andPage:withSize:options:

Get the image for a certain document page. Will first check the memory cache, then the disk cache and lastly queues a request to render. Returns the image instantly if the memory cache was filled, else will queue and call the delegate. If requireExactSize is set, images will either be downscaled or dynamically rendered. (There’s no point in upscaling)

- (UIImage *)imageFromDocument:(PSPDFDocument *)document andPage:(NSUInteger)page withSize:(CGSize)size options:(PSPDFCacheOptions)options

Discussion

Note: The cache will always return an aspect ratio corrected size of the image, so resulting size might be different.

Declared In

PSPDFCache.h

invalidateImageFromDocument:andPage:

Allows to invalidate a single page in the document. This usually is called after an annotation changes (and thus the image needs to be re-rendered)

- (void)invalidateImageFromDocument:(PSPDFDocument *)document andPage:(NSUInteger)page

Declared In

PSPDFCache.h

pauseCachingForService:

Will pause queued cache requests on the render queue. For service use the class object that requests the pause.

- (BOOL)pauseCachingForService:(id)service

Return Value

Returns YES if the cache has been paused.

Declared In

PSPDFCache.h

removeCacheForDocument:deleteDocument:error:

Removes the whole cache (memory/disk) for document. Will cancel any open writes as well. Enable deleteDocument to remove the document and the associated metadata.

- (BOOL)removeCacheForDocument:(PSPDFDocument *)document deleteDocument:(BOOL)deleteDocument error:(NSError **)error

Declared In

PSPDFCache.h

removeDelegate:

Deregisters a delegate.

- (BOOL)removeDelegate:(id<PSPDFCacheDelegate>)aDelegate

Return Value

Returns YES on success.

Declared In

PSPDFCache.h

resumeCachingForService:

Will resume queued cache requests on the render queue. For service use the class object that requested the pause.

- (BOOL)resumeCachingForService:(id)service

Return Value

Returns YES if the cache has been resumed.

Declared In

PSPDFCache.h

saveImage:fromDocument:andPage:withReceipt:

Caches the image in memory and disk for later re-use. PSPDFCache will decide at runtime if the image is worth saving into memory or just disk. (And disk will only be hit if the image is different)

- (void)saveImage:(UIImage *)image fromDocument:(PSPDFDocument *)document andPage:(NSUInteger)page withReceipt:(NSString *)renderReceipt

Declared In

PSPDFCache.h

stopCachingDocument:

Stops all cache requests (render requests, queued disk writes) for the document.

- (void)stopCachingDocument:(PSPDFDocument *)document

Declared In

PSPDFCache.h