IOS: Local Image Storage, local retrieval, and local deletion. ios Retrieval
IOS image local storage, local retrieval, and local Deletion
Image localization is often used in iOS development. IOS images are stored locally, obtained locally, and deleted locally. You can use the following methods.
// Save the image to a local device
+ (Void) SaveImageToLocal :( UIImage *) image Keys :( NSString *) key {
// First, obtain the sandbox path
NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];
NSLog (@ "Save the image locally % @", picPath );
BOOL isHaveImage = [self LocalHaveImage: key];
If (isHaveImage ){
NSLog (@ "the image has been saved locally, and you do not need to store it again ...");
Return;
}
NSData * imgData = UIImageJPEGRepresentation (image, 0.5 );
[ImgData writeToFile: picPath atomically: YES];
}
// Obtain the image locally
+ (UIImage *) GetImageFromLocal :( NSString *) key {
If ([JKBlankTool isBlankString: key]) {
Return nil;
}
// Read the local image instead of resource
NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];
NSLog (@ "get image % @", picPath );
UIImage * img = [[UIImage alloc] initWithContentsOfFile: picPath];
Return img;
}
// Check whether images exist locally
+ (BOOL) LocalHaveImage :( NSString *) key {
If ([JKBlankTool isBlankString: key]) {
Return NO;
}
// Read the local image instead of resource
NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];
NSLog (@ "check whether there are % @", picPath );
UIImage * img = [[UIImage alloc] initWithContentsOfFile: picPath];
If (img ){
Return YES;
}
Return NO;
}
// Delete the image from the local device
+ (Void) RemoveImageToLocalKeys :( NSString *) key {
NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];
NSLog (@ "Delete the image from the local device % @", picPath );
[[NSFileManager defaultManager] removeItemAtPath: picPath error: nil];
}