Wheat Academy iOS notes: Several ways iOS caches pictures locally

Source: Internet
Author: User

put iOS How many images are cached locally? Now , what are some of the ways students learn about iOS development notes in the Wheat Academy?

<code> cache the pictures locally, in a lot of scenes will be used, if only to store text information, then build a plist files, or databases, can be very convenient to solve problems, but if you save the picture to the sandbox is not so convenient. Here are two ways to save a picture to a sandbox.

</code>

I. Convert the image into a base64 string into a database or plist file, and then remove it when you use it.

<codeclass= "Hljs" objectivec= "" >// Gets the sandbox path,

NSString *path_sandox = Nshomedirectory ();

    // Create a storage plist path to the file

NSString *newpath = [Path_sandoxstringbyappendingpathcomponent:@/documents/pic.plist];

Nsmutablearray *arr = [[Nsmutablearray alloc]init];

    // convert the picture to Base64 the string

NSString *image64 = [Selfencodetobase64string:image];

[Arr addobject:image64];

    // Write plist file

if ([arr WriteToFile:newPathatomically:YES]) {

NSLog (@ write succeeded );

};

</code>

And then save it and use it to convert the stored string into a picture.

<codeclass= "Hljs" perl= "" >nsdata *_decodedimagedata = [[NSData alloc]initwithbase64encoding:image64];

UIImage *_decodedimage =[uiimage imagewithdata:_decodedimagedata];</code>

Two. Save the picture directly to the sandbox, and then store the path, and then take the path to the image and get the picture by the path.

<code class= "Hljs" objectivec= "" >// get the picture .

UIImage *image = [UIImage imagenamed: @flower. png]; Nsstring*path_sandox = Nshomedirectory ();

// Set the storage path for a picture

NSString *imagepath = [Path_sandoxstringbyappendingstring:@/documents/flower.png];

// Save the picture directly to the specified path (you should also ImagePath the path to the image save it, you can use it directly next time .

[Uiimagepngrepresentation (image) writetofile:imagepathatomically:yes];</code>

Next time use the address of the picture to get the picture directly.
Also attach the code to get the sandbox directory
Sandbox file directory get code:

Home Directory

<code class= "Hljs" fix= "" >nsstring*homedirectory = Nshomedirectory ();</code>

Document Directory

<code class= "Hljs" objectivec= "" >nsarray*paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString *path = [Paths objectatindex:0];</code>

Cache Directory

<code class= "Hljs" objectivec= "" >nsarray*paths = Nssearchpathfordirectoriesindomains (NSCachesDirectory, Nsuserdomainmask, YES);

NSString *path = [Paths objectatindex:0]; </code>

Libaray Directory

<code class= "Hljs" objectivec= "" >nsarray*paths = Nssearchpathfordirectoriesindomains (NSLibraryDirectory, Nsuserdomainmask, YES);

NSString *path = [Paths objectatindex:0];</code>

More Wheat Academy iOS Development Tutorials and notes at:http://www.maiziedu.com/course/ios/

Wheat Academy iOS notes: Several ways iOS caches pictures locally

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.