In iOS development, it is often used to localize images. IOS image local storage, local fetch, local delete, can be implemented by the following class methods.
Save a picture to a local
+ (void) saveimagetolocal: (uiimage*) image Keys: (nsstring*) key {
First, you need to get the sandbox path
nsstring *picpath=[nsstring stringwithformat:@ "%@/documents/%@.png", Nshomedirectory(), key];
NSLog(@ "Save picture to local %@", Picpath);
BOOL ishaveimage = [self localhaveimage:key];
if (ishaveimage) {
NSLog(@ "Local has saved the picture, no need to store it again ...");
return;
}
nsdata *imgdata = uiimagejpegrepresentation(image,0.5);
[Imgdata writetofile:picpath atomically:YES];
}
Get pictures from local
+ (uiimage*) getimagefromlocal: (nsstring*) key {
if ([jkblanktool Isblankstring:key]) {
return nil;
}
Read local picture non-resource
nsstring *picpath=[nsstring stringwithformat:@ "%@/documents/%@.png", Nshomedirectory (), key];
NSLog (@ "Get picture %@", Picpath);
UIImage *img=[[UIImage alloc]initwithcontentsoffile:p Icpath];
return img;
}
Do you have a picture locally?
+ (BOOL) Localhaveimage: (nsstring*) key {
if ([jkblanktool Isblankstring:key]) {
return NO;
}
Read local picture non-resource
nsstring *picpath=[nsstring stringwithformat:@ "%@/documents/%@.png", Nshomedirectory(), key];
NSLog (@ "Query for Presence%@", picpath);
UIImage *img=[[UIImage alloc]initwithcontentsoffile:p Icpath];
if (img) {
return YES;
}
return NO;
}
Remove a picture from the local
+ (void) Removeimagetolocalkeys: (nsstring*) key {
nsstring *picpath=[nsstring stringwithformat:@ "%@/documents/%@.png", Nshomedirectory (), key];
NSLog(@ "Remove picture from local %@", Picpath);
[[Nsfilemanager defaultmanager] removeItemAtPath:p icpath error:nil];
}
IOS picture Local storage, local fetch, local deletion