SoFunction
Updated on 2025-04-12

Several ways to cache images locally in iOS (summary)

Caching images locally is used in many scenarios. If you just store file information, then creating a plist file or database can easily solve the problem, but if you store images in the sandbox, it is not that convenient. Here are two ways to save pictures to the sandbox.

1. Convert the image to base64 string and store it in the database or plist file, and then take it out when used.

 //Get the sandbox path, NSString *path_sandox = NSHomeDirectory();
 //Create a path to store plist files NSString *newPath = [path_sandox stringByAppendingPathComponent:@/Documents/];
 NSMutableArray *arr = [[NSMutableArray alloc] init];
 UIImage *image = [UIImage imageNamed:@""];
  
 /*
   Convert the image to a Base64 string

   There are two simple ways to read image data on an iphone: UIImageJPEGRepresentation and UIImagePNGRepresentation.
  
   The UIImageJPEGRepresentation function requires two parameters: the image reference and the compression coefficient. UIImagePNGRepresentation only requires the image reference as the parameter. Through the actual use process,
   Comparatively, it was found that UIImagePNGRepresentation(UIImage* image) is much larger than UIImageJPEGRepresentation(UIImage* image, 1.0).
   For example, when reading photos of the same scenery taken by the camera, the data size returned by UIImagePNGRepresentation() is 199K.
   The data size returned by UIImageJPEGRepresentation(UIImage* image, 1.0) is only 140KB, which is more than 50KB less than the former.
   If the clarity of the picture is not high, you can also set the second parameter of the UIImageJPEGRepresentation function to greatly reduce the amount of image data. For example, the picture taken just now,
   When reading data by calling UIImageJPEGRepresentation(UIImage* image, 1.0), the returned data size is 140KB, but after changing the compression coefficient,
   When reading data by calling UIImageJPEGRepresentation(UIImage* image, 0.5), the returned data size is only more than 11KB, which greatly compresses the data volume of the image.
   Moreover, from a perspective perspective, the quality of the picture has not been significantly reduced. Therefore, when reading the image data content, it is recommended to give priority to using UIImageJPEGRepresentation.
   You can also set the compression coefficient according to your actual usage scenario to further reduce the size of the image data.
   */
 NSData *_data = UIImageJPEGRepresentation(image, 1.0f);
 //Convert the image data into a string NSString *strimage64 = [_data base64EncodedString];
 
 [arr addObject:image64]; 
  //Write to plist file if ([arr writeToFile:newPath atomically:YES]) {  
 NSLog(@"Writing successfully"); 
 };
  //You can view the picture data in the plist file under the Shahe path //This way it is saved, and then when used, use the stored string to convert it into a picture //NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:image64]; This is a method before iOS7 
 NSData *_decodedImageData = [[NSData alloc]initWithBase64EncodedString:strimage64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
 UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
 
 //You can print the image or not. NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));

2. Save the picture directly into the sandbox, and then store the path. When using the picture, first get the path of the picture, and then get the picture through the path.

 //Get the picture UIImage *image2 = [UIImage imageNamed:@""]; 
 NSString *path_document = NSHomeDirectory();
 //Set the storage path of an image NSString *imagePath = [path_document stringByAppendingString:@"/Documents/"];
 //Save the image directly to the specified path (at the same time, the image path imagePath should be saved, and you can use it directly to retrieve it next time) [UIImagePNGRepresentation(image2) writeToFile:imagePath atomically:YES];

Next time, use the address of the picture to get the picture directly.

UIImage *getimage2 = [UIImage imageWithContentsOfFile:imagePath]; 
NSLog(@"image2 is size %@",NSStringFromCGSize()); 

Attach the code to get the sandbox directory

Sandbox file directory get code:

//Home directory

NSString *homeDirectory = NSHomeDirectory(); 

//Document directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *path = [paths objectAtIndex:0]; 

//Cache directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *path = [paths objectAtIndex:0]; 

//Library directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
NSString *path = [paths objectAtIndex:0];

The above article (summary) of iOS cache images locally is all the content I share with you. I hope you can give you a reference and I hope you support me more.