Picture Cache to Sandbox (custom method, suitable for use when UIImage objects have been acquired)

Source: Internet
Author: User

The first is a MD5 encrypted class, the absolute or relative path of the picture is encrypted with MD5, because there are many "/" in the path, the direct storage will be problematic

First to import the header file

#import <CommonCrypto/CommonCrypto.h>

Then the encapsulated MD5 method of encryption

+ (NSString *) MD5: (NSString *) str{Const Char*cstr =[str utf8string]; unsignedCharresult[ -]; CC_MD5 (CSTR, (unsignedint) strlen (CSTR), result);//The MD5 call    return[NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[Ten], result[ One], result[ A], result[ -], result[ -], result[ the]            ];}

And then there's the way to store the pictures.

+ (void) Savesmallimagewithimage: (uiimage*) Image Url: (nsstring*) imageUrl atdirectory: (nsstring*) directory{//creating a File ManagerNsfilemanager *filemanager =[Nsfilemanager Defaultmanager]; //1. Splicing CatalogueNSString *path =[Nshomedirectory () stringbyappendingstring:directory]; NSString* Savepath = [path stringbyappendingstring:[nsstring stringWithFormat:@"/%@.jpg", [self md5:imageurl]];    [FileManager Changecurrentdirectorypath:savepath]; NSLog (@"%@", Savepath);
[FileManager Createdirectoryatpath:path withintermediatedirectories:yes Attributes:nil Error:nil]; if(! [FileManager Fileexistsatpath:savepath]) {//determine if there is//Createfileatpath://The path to the file created by the first parameter (full path includes file name and suffix)//the contents of the second parameter file (in general, the contents of the newly created file are null nil)//the third Parameter property permission is generally nil represents the default permission for the file management classBOOL ret =[FileManager Createfileatpath:savepath contents:uiimagepngrepresentation (image) Attributes:nil]; if(!ret) {NSLog (@ "PictureFile creation failed"); } }}

The first parameter in this method is a UIImage object, directly to the picture you want to cache as UIImage object passed in, ImageUrl is generally the absolute or relative path of the picture, of course, you can also join their own or directly write a path to the line, Directory is the directory you want to store, we all know that nshomedirectory() This property can only channel the root directory of the sandbox, the next path needs their own splicing, if you want to directly into the sandbox can also, directory empty on the line, If you want to write your own path, for example, I would like to store it in the Cacheimage directory under the SHA-Hanoi Documents directory, and I will be writing directory "Documents/cacheimage" on the line ...

And then it's the way to take out the picture.

+ (uiimage*) Getsmallimagewithurl: (nsstring*) imageUrl atdirectory: (nsstring*) directory{//creating a File ManagerNsfilemanager *filemanager =[Nsfilemanager Defaultmanager]; //Get Path//1. Splicing CatalogueNSString *path =[Nshomedirectory () stringbyappendingstring:directory]; NSString* Savepath = [path stringbyappendingstring:[nsstring stringWithFormat:@"/%@.jpg", [self md5:imageurl]];        [FileManager Changecurrentdirectorypath:savepath]; NSData* ImageData =[FileManager Contentsatpath:savepath]; if(!imageData) {NSLog (@ "PictureFile acquisition failed"); }        return[UIImage imagewithdata:imagedata];}

The meaning of the parameter is the same as the storage method, except that the stored picture is changed to return an image, so that you can cache the image according to your own idea ...

Welcome everyone to make suggestions and comments, we learn together, progress together ...

Picture Cache to Sandbox (custom method, suitable for use when UIImage objects have been acquired)

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.