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
- //Get 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 image to Base64 string
- There are two simple ways to read image data on iphone: uiimagejpegresentation and UIImagePNGRepresentation
- UIImageJPEGRepresentation The function requires two parameters: picture reference and compression coefficient. UIImagePNGRepresentation only requires picture reference as a parameter. In the actual use process,
- The comparison shows that UIImagePNGRepresentation(UIImage * image) has much more image data than UIImageJPEGRepresentation(UIImage * image, 1.0)
- For example, if you want to read photos of the same scene taken by the camera, UIImagePNGRepresentation() will return 199K data,
- UIImageJPEGRepresentation(UIImage * image, 1.0) only returns 140KB of data, which is more than 50 KB less than the former
- 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
- When reading data by calling UIImageJPEGRepresentation(UIImage * image, 1.0), the returned data size is 140KB, but after changing the compression coefficient,
- 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,
- 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,
- And according to their own actual use scenarios, set compression coefficient, further reduce the size of image data
- */
- NSData *_data = UIImageJPEGRepresentation(image, 1.0f);
- //Convert the data of the picture to a string
- NSString *strimage64 = [_data base64EncodedString];
- [arr addObject:image64];
- //Write plist file
- if ([arr writeToFile:newPath atomically:YES]) {
- NSLog(@"Write successfully");
- };
- //You can view the picture data in plist file in Shahe path
- //In this way, it can be saved, and then it can be used to convert the stored string into a picture
- //NSData * u decodedImageData = [[NSData alloc] initWithBase64Encoding:image64]; this is a method before iOS7
- NSData *_decodedImageData = [[NSData alloc]initWithBase64EncodedString:strimage64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
- UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
- //Can print whether the following 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 pictures
- UIImage *image2 = [UIImage imageNamed:@"1.png"];
- NSString *path_document = NSHomeDirectory();
- //Set the storage path of a picture
- NSString *imagePath = [path_document stringByAppendingString:@"/Documents/pic.png"];
- //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)
- [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));
Also attach the code to get the sandbox directory
Sandbox file directory access 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];
//Libaray directory
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];