PSPDFCache


@interface PSPDFCache : NSObject

The PSPDFCache is responsible for managing the memory and disk cache of rendered images.

Usually you do not access any methods of PSPDFCache directly but instead schedule a PSPDFRenderTask in a PSPDFRenderQueue which will then reach out to the cache and check if there are images available before rendering a new one.

See

PSPDFRenderTask
  • Undocumented

    Declaration

    Objective-C

    
    @interface PSPDFCache : NSObject
  • Undocumented

    Declaration

    Objective-C

    
    @interface PSPDFCache : NSObject
  • The designated initializer.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithSettings:(nonnull PSPDFKit *)pspdfkit;

    Swift

    init(settings pspdfkit: PSPDFKit)
  • 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)

    Declaration

    Objective-C

    - (void)saveImage:(nonnull UIImage *)image
           forRequest:(nonnull PSPDFRenderRequest *)request;

    Swift

    func save(_ image: UIImage, for request: PSPDFRenderRequest)
  • Asynchronously pre-renders and caches the document. The delegate method didRenderImage:document:page:size: gets called after each image is rendered (number of pages x number of sizes).

    Note

    Under certain conditions (such as if the device is running low on power) the cache may suspend pre caching operations until everything has been restored to normal conditions.

    Declaration

    Objective-C

    - (void)cacheDocument:(nullable PSPDFDocument *)document
                    pageSizes:(nonnull NSArray<NSValue *> *)sizes
        withDiskCacheStrategy:(PSPDFDiskCacheStrategy)strategy
            aroundPageAtIndex:(NSUInteger)pageIndex;

    Swift

    func cacheDocument(_ document: PSPDFDocument?, pageSizes sizes: [NSValue], with strategy: PSPDFDiskCacheStrategy, aroundPageAt pageIndex: UInt)

    Parameters

    document

    The document to render and cache — if nil, this message is ignored.

    sizes

    An array of NSValue objects constructed with CGSize. Each page will be rendered for each size specified in this array.

    strategy

    The caching strategy to use.

    pageIndex

    If using PSPDFDiskCacheStrategyNearPages a few pages before and after the provided page will be cached only. The parameter is otherwise ignored.

  • Asynchronously pre-renders and caches the document. The delegate method didRenderImage:document:page:size: gets called after each image is rendered (number of pages x number of sizes).

    Note

    Under certain conditions (such as if the device is running low on power) the cache may suspend pre caching operations until everything has been restored to normal conditions.

    Declaration

    Objective-C

    - (void)cacheDocument:(nullable PSPDFDocument *)document
                            pageSizes:(nonnull NSArray<NSValue *> *)sizes
                withDiskCacheStrategy:(PSPDFDiskCacheStrategy)strategy
                    aroundPageAtIndex:(NSUInteger)pageIndex
        imageRenderingCompletionBlock:
            (nullable PSPDFCacheDocumentImageRenderingCompletionBlock)
                pageCompletionBlock;

    Swift

    func cacheDocument(_ document: PSPDFDocument?, pageSizes sizes: [NSValue], with strategy: PSPDFDiskCacheStrategy, aroundPageAt pageIndex: UInt, imageRenderingCompletionBlock pageCompletionBlock: PSPDFCacheDocumentImageRenderingCompletionBlock? = nil)

    Parameters

    document

    The document to render and cache — if nil, this message is ignored.

    sizes

    An array of NSValue objects constructed with CGSize. Each page will be rendered for each size specified in this array.

    strategy

    The caching strategy to use.

    pageIndex

    If using PSPDFDiskCacheStrategyNearPages a few pages before and after the provided page will be cached only. The parameter is otherwise ignored.

    pageCompletionBlock

    This block will be executed each time a page is rendered for each size (the delegates, if any, will still be called!).

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

    Declaration

    Objective-C

    - (void)stopCachingDocument:(nullable PSPDFDocument *)document;

    Swift

    func stopCachingDocument(_ document: PSPDFDocument?)
  • 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) - note: If the document is nil, the request is silently ignored.

    Declaration

    Objective-C

    - (void)invalidateImageFromDocument:(nullable PSPDFDocument *)document
                              pageIndex:(NSUInteger)pageIndex;

    Swift

    func invalidateImage(from document: PSPDFDocument?, pageIndex: UInt)
  • 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.

    Declaration

    Objective-C

    - (void)removeCacheForDocument:(nullable PSPDFDocument *)document;

    Swift

    func remove(for document: PSPDFDocument?)
  • Clears the disk and memory cache.

    Declaration

    Objective-C

    - (void)clearCache;

    Swift

    func clear()
  • Cache files are saved in a subdirectory of NSCachesDirectory. Defaults to PSPDFKit/Pages. - 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 cache before changing.

    Declaration

    Objective-C

    @property (readwrite, copy, nonatomic) NSString *_Nonnull cacheDirectory;

    Swift

    var cacheDirectory: String { get set }
  • Defines the global disk cache strategy. Defaults to PSPDFDiskCacheStrategyEverything. If PSPDFDocument also defines a strategy, that one is prioritized.

    Declaration

    Objective-C

    @property (assign, readwrite, nonatomic)
        PSPDFDiskCacheStrategy diskCacheStrategy;

    Swift

    var diskCacheStrategy: PSPDFDiskCacheStrategy { get set }
  • The maximum amount of disk space the cache is allowed to use (in bytes).

    This value is a non strict maximum value. The cache might also start evicting images before this limit is reached, depending on the memory and disk state of the device.

    Note

    Set to 0 to disable the disk cache.

    Declaration

    Objective-C

    @property (assign, readwrite, nonatomic) unsigned long long allowedDiskSpace;

    Swift

    var allowedDiskSpace: UInt64 { get set }
  • The disk space currently used by the cache (in bytes).

    Declaration

    Objective-C

    @property (readonly, nonatomic) unsigned long long usedDiskSpace;

    Swift

    var usedDiskSpace: UInt64 { get }
  • 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.

    Declaration

    Objective-C

    @property (assign, readwrite, nonatomic) BOOL useJPGFormat;

    Swift

    var useJPGFormat: Bool { get set }
  • Compression strength for JPG. (PNG is loss-less) 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.

    Declaration

    Objective-C

    @property (assign, readwrite, nonatomic) CGFloat JPGFormatCompression;

    Swift

    var jpgFormatCompression: CGFloat { get set }
  • Decrypt data from the path. Requires the PSPDFFeatureMaskStrongEncryption feature flag. If set to nil, the default implementation will be used.

    Declaration

    Objective-C

    @property (readwrite, copy, atomic) NSData *_Nonnull (^_Nonnull)
        (PSPDFDocument *_Nonnull, NSString *_Nonnull) decryptFromPathBlock;

    Swift

    var decryptFromPathBlock: (PSPDFDocument, String) -> Data { get set }
  • Encrypt mutable data. Requires the PSPDFFeatureMaskStrongEncryption feature flag. If set to nil, the default implementation will be used.

    Declaration

    Objective-C

    @property (readwrite, copy, atomic) void (^_Nonnull)
        (PSPDFDocument *_Nonnull, NSMutableData *_Nonnull) encryptDataBlock;

    Swift

    var encryptDataBlock: (PSPDFDocument, NSMutableData) -> Void { get set }
  • Will pause queued cache requests on the render queue. For service use the class object that requests the pause.

    Declaration

    Objective-C

    - (void)pauseCachingForService:(nonnull id)service;

    Swift

    func pauseCaching(forService service: Any)
  • Will resume queued cache requests on the render queue. For service use the class object that requested the pause.

    Declaration

    Objective-C

    - (void)resumeCachingForService:(nonnull id)service;

    Swift

    func resumeCaching(forService service: Any)