IOS picture local storage, local fetch, local delete

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.