Several methods for IOS to cache images locally (summary)

Time:2020-10-19

Caching images locally can be used in many scenarios. If only file information is stored, it is convenient to create a plist file or database to solve the problem, but it is not so convenient to store images in sandbox. Here are two ways to save pictures to sandbox.

1、 Convert the image to Base64 string and save it in the database or plist file, and then take it out when you use it

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

  There are two simple ways to read image data on iPhone: uiimagejpegrepresentation and uiimagepngrepresentation 
  
  In the actual process, we need to use the image representation coefficient as the image representation parameter,
  It is found that uiimage pngrepresentation (uiimage * image) returns much more image data than uiimagejpegrepresentation (uiimage * image, 1.0)
  For example, if you read the photos of the same scenery taken by the camera, the size of the data returned by uiimagepngrepresentation() is 199k,
  However, the size of data returned by uiimage jpegrepresentation (uiimage * image, 1.0) is only 140kb, which is more than 50 KB less than the former
  If the clarity of the image is not required, you can also set the second parameter of uiimagejpegrepresentation function to greatly reduce the amount of image data. For example, for the image just taken, 
  When reading data by calling uiimagejpegrepresentation (uiimage * image, 1.0), the returned data size is 140kb, but after changing the compression factor, the size of the returned data is 140kb,
  When reading data by calling uiimagejpegrepresentation (uiimage * image, 0.5), the returned data size is only more than 11KB, which greatly reduces the amount of image data,
  Therefore, when reading the content of image data, it is recommended to use uiimagejpegrepresentation first,
  The compression coefficient can be set according to the actual scene to further reduce the image data size
  */
 NSData *_data = UIImageJPEGRepresentation(image, 1.0f);
 //Convert the data of the image to a string
 NSString *strimage64 = [_data base64EncodedString];
 
 [arr addObject:image64]; 
  //Write to plist file 
 if ([arr writeToFile:newPath atomically:YES]) {  
 Nslog (@ "write succeeded"); 
 };
  //You can view the image data in plist file under Shahe path 
 //This will be saved, and then used to convert the stored string into an image
 //NSData *_ decodedImageData = [[NSData alloc] initWithBase64 Encoding:image64 ]; this is a method before ios7 
 
 NSData *_decodedImageData = [[NSData alloc]initWithBase64EncodedString:strimage64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
 UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
 
 //You can print whether the picture exists
 NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));

2、 Save the image directly to the sandbox, and then store the path. When using the image, first get the path of the image, and then get the image through the path

//Get the picture
 UIImage *image2 = [UIImage imageNamed:@"1.png"]; 
 NSString *path_document = NSHomeDirectory();
 //Set the storage path of an image
 NSString *imagePath = [path_document stringByAppendingString:@"/Documents/pic.png"];
 //Save the image directly to the specified path (at the same time, the image path should be saved, which can be used 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(getimage2.size)); 

The code for getting the sandbox catalog is also attached

Sandbox file directory access code:

//Home directory


NSString *homeDirectory = NSHomeDirectory(); 

//Document contents


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

//Cache directory


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

//Libaray catalog


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

The above several methods (summary) of IOS caching images to local are all the contents shared by Xiaobian. I hope to give you a reference, and I hope you can support developeppaer more.