Skip to content

Commit

Permalink
Merge pull request #128 from Sega-Zero/master
Browse files Browse the repository at this point in the history
Better swift support
  • Loading branch information
mallorypaine authored Sep 13, 2016
2 parents 0b00b01 + 7cf6b35 commit 2615d27
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 25 deletions.
11 changes: 8 additions & 3 deletions FastImageCache/FastImageCache/FastImageCache/FICEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "FICImports.h"
@class FICImageFormat;

NS_ASSUME_NONNULL_BEGIN

typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextSize);

/**
Expand Down Expand Up @@ -52,7 +54,8 @@ typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextS
@see FICImageFormat
@see [FICImageCacheDelegate imageCache:wantsSourceImageForEntity:withFormatName:completionBlock:]
*/
- (NSURL *)fic_sourceImageURLWithFormatName:(NSString *)formatName;
- (nullable NSURL *)fic_sourceImageURLWithFormatName:(NSString *)formatName;


/**
Returns the drawing block for a specific image and format name.
Expand All @@ -73,14 +76,16 @@ typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextS
@note This block will always be called from the serial dispatch queue used by the image cache.
*/
- (FICEntityImageDrawingBlock)fic_drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName;
- (nullable FICEntityImageDrawingBlock)fic_drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName;

@optional
/**
Returns the image for a format
@param format The image format that identifies which image table is requesting the source image.
*/
- (UIImage *)fic_imageForFormat:(FICImageFormat *)format;
- (nullable UIImage *)fic_imageForFormat:(FICImageFormat *)format;

@end

NS_ASSUME_NONNULL_END
22 changes: 13 additions & 9 deletions FastImageCache/FastImageCache/FastImageCache/FICImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
@protocol FICEntity;
@protocol FICImageCacheDelegate;

typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image);
typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> _Nullable entity, NSString * _Nonnull formatName, UIImage * _Nullable image);
typedef void (^FICImageRequestCompletionBlock)(UIImage * _Nullable sourceImage);

NS_ASSUME_NONNULL_BEGIN

/**
`FICImageCache` is the primary class for managing and interacting with the image cache. Applications using the image cache create one or more `<FICImageFormat>`
Expand Down Expand Up @@ -98,7 +100,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
@note Once the image formats have been set, subsequent calls to this method will do nothing.
*/
- (void)setFormats:(NSArray *)formats;
- (void)setFormats:(NSArray<FICImageFormat*> *)formats;

/**
Returns an image format previously associated with the image cache.
Expand All @@ -107,7 +109,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
@return An image format with the name `formatName` or `nil` if no format with that name exists.
*/
- (FICImageFormat *)formatWithName:(NSString *)formatName;
- (nullable FICImageFormat *)formatWithName:(NSString *)formatName;

/**
Returns all the image formats of the same family previously associated with the image cache.
Expand All @@ -116,7 +118,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
@return An array of `<FICImageFormat>` objects whose family is `family` or `nil` if no format belongs to that family.
*/
- (NSArray *)formatsWithFamily:(NSString *)family;
- (nullable NSArray<FICImageFormat *> *)formatsWithFamily:(NSString *)family;

///-----------------------------------------------
/// @name Storing, Retrieving, and Deleting Images
Expand All @@ -141,7 +143,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image)
*/
- (void)setImage:(UIImage *)image forEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock;
- (void)setImage:(UIImage *)image forEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageCacheCompletionBlock)completionBlock;

/**
Attempts to synchronously retrieve an image from the image cache.
Expand All @@ -168,7 +170,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
@note You can always rely on the completion block being called. If an error occurs for any reason, the `image` parameter of the completion block will be `nil`. See
<[FICImageCacheDelegate imageCache:errorDidOccurWithMessage:]> for information about being notified when errors occur.
*/
- (BOOL)retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock;
- (BOOL)retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageCacheCompletionBlock)completionBlock;

/**
Asynchronously retrieves an image from the image cache.
Expand All @@ -193,7 +195,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
@see [FICImageCache retrieveImageForEntity:withFormatName:completionBlock:]
*/
- (BOOL)asynchronouslyRetrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock;
- (BOOL)asynchronouslyRetrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageCacheCompletionBlock)completionBlock;

/**
Deletes an image from the image cache.
Expand Down Expand Up @@ -281,7 +283,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
the URL returned by <[FICEntity sourceImageURLWithFormatName:]>, deserializing the image data when the request completes, and finally calling this method's completion
block to provide the image cache with the source image.
*/
- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageRequestCompletionBlock)completionBlock;
- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageRequestCompletionBlock)completionBlock;

/**
This method is called on the delegate when the image cache has received an image retrieval cancellation request.
Expand Down Expand Up @@ -332,3 +334,5 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
- (void)imageCache:(FICImageCache *)imageCache errorDidOccurWithMessage:(NSString *)errorMessage;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef NS_ENUM(NSUInteger, FICImageFormatProtectionMode) {
prevent the image cache from consuming too much disk space. Each `<FICImageTable>` managed by the image cache is associated with a single image format.
*/

NS_ASSUME_NONNULL_BEGIN
@interface FICImageFormat : NSObject <NSCopying>

///------------------------------
Expand Down Expand Up @@ -152,7 +153,7 @@ typedef NS_ENUM(NSUInteger, FICImageFormatProtectionMode) {
@discussion Fast Image Cache automatically serializes the image formats that it uses to disk. If an image format ever changes, Fast Image Cache automatically detects the change and invalidates the
image table associated with that image format. The image table is then recreated from the updated image format.
*/
@property (nonatomic, copy, readonly) NSDictionary *dictionaryRepresentation;
@property (nonatomic, copy, readonly) NSDictionary<NSString*, id> *dictionaryRepresentation;

///-----------------------------------
/// @name Initializing an Image Format
Expand Down Expand Up @@ -180,3 +181,4 @@ typedef NS_ENUM(NSUInteger, FICImageFormatProtectionMode) {
+ (instancetype)formatWithName:(NSString *)name family:(NSString *)family imageSize:(CGSize)imageSize style:(FICImageFormatStyle)style maximumCount:(NSInteger)maximumCount devices:(FICImageFormatDevices)devices protectionMode:(FICImageFormatProtectionMode)protectionMode;

@end
NS_ASSUME_NONNULL_END
10 changes: 8 additions & 2 deletions FastImageCache/FastImageCache/FastImageCache/FICImageTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@class FICImageTableEntry;
@class FICImage;

NS_ASSUME_NONNULL_BEGIN

extern NSString *const FICImageTableEntryDataVersionKey;
extern NSString *const FICImageTableScreenScaleKey;

Expand Down Expand Up @@ -81,7 +83,9 @@ extern NSString *const FICImageTableScreenScaleKey;
@warning `FICImageTable` raises an exception if `imageFormat` is `nil`. `FICImageTable`'s implementation of `-init` simply calls through to this initializer, passing `nil` for `imageFormat`.
*/
- (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImageCache *)imageCache;
- (nullable instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImageCache *)imageCache NS_DESIGNATED_INITIALIZER;
-(instancetype) init __attribute__((unavailable("Invoke the designated initializer initWithFormat:imageCache: instead")));
+(instancetype) new __attribute__((unavailable("Invoke the designated initializer initWithFormat:imageCache: instead")));

///------------------------------------------------
/// @name Storing, Retrieving, and Deleting Entries
Expand Down Expand Up @@ -123,7 +127,7 @@ extern NSString *const FICImageTableScreenScaleKey;
@note If either the entity UUID or the source image UUID doesn't match the corresponding UUIDs in the entry data, then something has changed. The entry data is deleted for the
provided entity UUID, and `nil` is returned.
*/
- (UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSString *)sourceImageUUID preheatData:(BOOL)preheatData;
- (nullable UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSString *)sourceImageUUID preheatData:(BOOL)preheatData;

/**
Deletes image entry data in the image table.
Expand Down Expand Up @@ -164,3 +168,5 @@ extern NSString *const FICImageTableScreenScaleKey;
- (void)reset;

@end

NS_ASSUME_NONNULL_END
4 changes: 0 additions & 4 deletions FastImageCache/FastImageCache/FastImageCache/FICImageTable.m
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImag
return self;
}

- (instancetype)init {
return [self initWithFormat:nil imageCache:nil];
}

- (void)dealloc {
if (_fileDescriptor >= 0) {
close(_fileDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "FICImports.h"

NS_ASSUME_NONNULL_BEGIN

@class FICImageTable;

/**
Expand Down Expand Up @@ -57,6 +59,8 @@
@return A new image table chunk.
*/
- (instancetype)initWithFileDescriptor:(int)fileDescriptor index:(NSInteger)index length:(size_t)length;
- (nullable instancetype)initWithFileDescriptor:(int)fileDescriptor index:(NSInteger)index length:(size_t)length;

@end

@end
NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "FICImports.h"

NS_ASSUME_NONNULL_BEGIN

@class FICImageTableChunk;
@class FICImageCache;

Expand Down Expand Up @@ -83,7 +85,7 @@ typedef struct {
@return A new image table entry.
*/
- (instancetype)initWithImageTableChunk:(FICImageTableChunk *)imageTableChunk bytes:(void *)bytes length:(size_t)length;
- (nullable instancetype)initWithImageTableChunk:(FICImageTableChunk *)imageTableChunk bytes:(void *)bytes length:(size_t)length;

/**
Adds a block to be executed when this image table entry is deallocated.
Expand Down Expand Up @@ -122,3 +124,5 @@ typedef struct {
+ (NSInteger)metadataVersion;

@end

NS_ASSUME_NONNULL_END
6 changes: 3 additions & 3 deletions FastImageCache/FastImageCache/FastImageCache/FICUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
size_t FICByteAlign(size_t bytesPerRow, size_t alignment);
size_t FICByteAlignForCoreAnimation(size_t bytesPerRow);

NSString * FICStringWithUUIDBytes(CFUUIDBytes UUIDBytes);
CFUUIDBytes FICUUIDBytesWithString(NSString *string);
CFUUIDBytes FICUUIDBytesFromMD5HashOfString(NSString *MD5Hash); // Useful for computing an entity's UUID from a URL, for example
NSString * _Nullable FICStringWithUUIDBytes(CFUUIDBytes UUIDBytes);
CFUUIDBytes FICUUIDBytesWithString(NSString * _Nonnull string);
CFUUIDBytes FICUUIDBytesFromMD5HashOfString(NSString * _Nonnull MD5Hash); // Useful for computing an entity's UUID from a URL, for example

0 comments on commit 2615d27

Please sign in to comment.