HYLImageManager is a convenience CRUD tool for image files in iOS file system. It is a simplified version of NSFileManager providing only simple CRUD methods with using a file name. This module is suitable for foundamental objective-c programmers who needs to save image or other file to persistent store.
It is simple enough to use, and also it is flexible enough to costomize.
For example, when you finish downloading a image, you may need to cache or store in local file system for later use. So you can just call [[HYLImageManager defaultManager] saveImage:image withImageName:@"demoImage.png"
. When you want to get it back, just call UIImage* image = [[HYLImageManager defaultManager] imageWithName:@"demoImage.png"
.
#Install ##1. CocoaPods (coming soon...) ##2. Drop-in Just Drop the "HYLImageManager" folder in you project.
Select one of the ninitializer below to get a HYLImageManager instance.
//Init the manager instance with the system directiory.
//All images stored will be stored in a folder called "UserDocuments", to its original size without thumbnail. It will not be compressed.
//The path will be "/<NSSearchPathDirectory>/UserDocuments/xxx.jpg"
-(instancetype)initWithDirectory:(NSSearchPathDirectory)directory;
//Init the manager with the system directory and folder name.
//All images stored will be stored in the path specified in the pathComponents, to its original size without thumbnail. It will not be compressed.
//The path will be "/<NSSearchPathDirectory>/pathComponents[0]/pathComponents[1]/pathComponents[2]/.../xxx.jpg"
-(instancetype)initWithDirectory:(NSSearchPathDirectory)directory pathComponents:(nullable NSArray *)pathComponents;
//Init the manager with the system directory, folder name and the maximum size of the image you want it be. When the image is saved to the disk, it will be resized to the maximum size you specify while keeping its ratio. If it's original size is smaller than the maximum size, it will not be resized.
//e.g. if you specify the maximum size to be (800, 600) (800 is width), and image's original size is (1000,1000), it will be resized to (600,600)
-(instancetype)initWithDirectory:(NSSearchPathDirectory)directory pathComponents:(NSArray *)pathComponents maxSize:(CGSize)maxSize;
//Init a image manager with most customization
-(instancetype)initWithDirectory:(NSSearchPathDirectory)directory pathComponents:(nullable NSArray *)pathComponents maxSize:(CGSize)maxSize thumbnailMaxSize:(CGSize)thumbnailMaxSize compressionQuality:(float)quality ignoreThumbnail:(BOOL)flag NS_DESIGNATED_INITIALIZER;
//Init a shared instance with default configuration.
+(instancetype)defaultManager;
-(UIImage *)imageWithName:(NSString *)imageName;
- (void)saveImage:(UIImage *)image withImageName:(NSString *)imageName;
Note, store the name or keep a reference of the name. You need use this name to fetch image again.
-(NSString *)saveImage:(UIImage *)image;
- (BOOL)deleteImageWithImageName:(NSString *)imageName error:(NSError **)error;
- (BOOL)renameImageFromImageName:(NSString *)oldImageName toNewImageName:(NSString *)newImageName error:(NSError **)error;
-(NSString *)pathForImageName:(NSString *)fileName;
-(BOOL)imageExistsForImageName:(NSString *)imageName;
For more details about how to use these methods, please refer to the header file.
This UIImage Category is used in all HYLImageManager to compress the image when the image is saved to disk. You can call the methods in this category whenever you like.
This is a more generic version of CRUD convenient tool targeting at NSData.