iOS realizes the function that the picture exists locally and then gets the picture locally

  • 2021-12-04 20:01:16
  • OfStack

iOS realizes the function of local picture existence and local picture acquisition. The code is as follows:


// Save pictures locally 
+ (void)SaveImageToLocal:(UIImage*)image Keys:(NSString*)key {
 NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
 //[preferences persistentDomainForName:LocalPath];
 [preferences setObject:UIImagePNGRepresentation(image) forKey:key];
}
 
// Are there any related pictures locally 
+ (BOOL)LocalHaveImage:(NSString*)key {
 NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
 //[preferences persistentDomainForName:LocalPath];
 NSData* imageData = [preferences objectForKey:key];
 if (imageData) {
  return YES;
 }
 return NO;
}
 
// Get pictures locally 
+ (UIImage*)GetImageFromLocal:(NSString*)key {
 NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
 //[preferences persistentDomainForName:LocalPath];
 NSData* imageData = [preferences objectForKey:key];
 UIImage* image;
 if (imageData) {
  image = [UIImage imageWithData:imageData];
 }
 else {
  NSLog(@" Picture not obtained locally ");
 }
 return image;
}

Related articles: