ios local various paths, cache, etc

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

1, Save the image as a base64 string in the database or plist file, and then retrieve it when needed

  1.  //Get sandbox path,  
  2.  NSString *path_sandox = NSHomeDirectory();  
  3.  //Create a path to store plist files  
  4.  NSString *newPath = [path_sandox stringByAppendingPathComponent:@/Documents/pic.plist];  
  5.  NSMutableArray *arr = [[NSMutableArray alloc] init];  
  6.  UIImage *image = [UIImage imageNamed:@"1.png"];  
  8.  /* 
  10.   Convert image to Base64 string 
  13.   There are two simple ways to read image data on iphone: uiimagejpegresentation and UIImagePNGRepresentation 
  15.   UIImageJPEGRepresentation The function requires two parameters: picture reference and compression coefficient. UIImagePNGRepresentation only requires picture reference as a parameter. In the actual use process, 
  16.   The comparison shows that UIImagePNGRepresentation(UIImage * image) has much more image data than UIImageJPEGRepresentation(UIImage * image, 1.0) 
  17.   For example, if you want to read photos of the same scene taken by the camera, UIImagePNGRepresentation() will return 199K data, 
  18.   UIImageJPEGRepresentation(UIImage * image, 1.0) only returns 140KB of data, which is more than 50 KB less than the former 
  19.   If the requirement of picture definition is not high, the second parameter of uiimagejpegresentation function can be set to greatly reduce the amount of picture data 
  20.   When reading data by calling UIImageJPEGRepresentation(UIImage * image, 1.0), the returned data size is 140KB, but after changing the compression coefficient, 
  21.   When UIImageJPEGRepresentation(UIImage * image, 0.5) is called to read data, the returned data size is only more than 11KB, which greatly reduces the data amount of pictures, 
  22.   In addition, from the perspective of view, the quality of the image does not significantly reduce. Therefore, when reading the image data content, it is recommended to give priority to uiimagejpegresentation, 
  23.   And according to their own actual use scenarios, set compression coefficient, further reduce the size of image data 
  24.   */  
  26.  NSData *_data = UIImageJPEGRepresentation(image, 1.0f);  
  27.  //Convert the data of the picture to a string  
  28.  NSString *strimage64 = [_data base64EncodedString];  
  30.  [arr addObject:image64];      
  31.   //Write plist file  
  32.  if ([arr writeToFile:newPath atomically:YES]) {         
  33.  NSLog(@"Write successfully");      
  34. };  
  35.  //You can view the picture data in plist file in Shahe path  
  37.  //In this way, it can be saved, and then it can be used to convert the stored string into a picture  
  39.  //NSData * u decodedImageData = [[NSData alloc] initWithBase64Encoding:image64]; this is a method before iOS7  
  41.  NSData *_decodedImageData = [[NSData alloc]initWithBase64EncodedString:strimage64 options:NSDataBase64DecodingIgnoreUnknownCharacters];  
  42.  UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];  
  44.  //Can print whether the following picture exists  
  45.  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

  1. //Get pictures  
  2. UIImage *image2 = [UIImage imageNamed:@"1.png"];   
  3. NSString *path_document = NSHomeDirectory();  
  4. //Set the storage path of a picture  
  5. NSString *imagePath = [path_document stringByAppendingString:@"/Documents/pic.png"];  
  6. //Save the image directly to the specified path (at the same time, you should save the path of the image imagePath, which can be used to retrieve it next time)  
  7. [UIImagePNGRepresentation(image2) writeToFile:imagePath atomically:YES];  

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

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

Also attach the code to get the sandbox directory

Sandbox file directory access code:

//Home directory

  1. NSString *homeDirectory = NSHomeDirectory();  

//Document directory
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
  2. NSString *path = [paths objectAtIndex:0];  

//Cache directory

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

//Libaray directory

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

Keywords: Database less

Added by yaron on Sun, 05 Apr 2020 13:34:35 +0300