IOS: Local Image Storage, local retrieval, and local deletion. ios Retrieval

Source: Internet
Author: User

IOS: Local Image Storage, local retrieval, and local deletion. ios Retrieval
IOS image local storage, local retrieval, and local Deletion

Image localization is often used in iOS development. IOS images are stored locally, obtained locally, and deleted locally. You can use the following methods.

// Save the image to a local device

+ (Void) SaveImageToLocal :( UIImage *) image Keys :( NSString *) key {

// First, obtain the sandbox path

NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];

NSLog (@ "Save the image locally % @", picPath );

BOOL isHaveImage = [self LocalHaveImage: key];

If (isHaveImage ){

NSLog (@ "the image has been saved locally, and you do not need to store it again ...");

Return;

}

NSData * imgData = UIImageJPEGRepresentation (image, 0.5 );

[ImgData writeToFile: picPath atomically: YES];

}

// Obtain the image locally

+ (UIImage *) GetImageFromLocal :( NSString *) key {

If ([JKBlankTool isBlankString: key]) {

Return nil;

}

// Read the local image instead of resource

NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];

NSLog (@ "get image % @", picPath );

UIImage * img = [[UIImage alloc] initWithContentsOfFile: picPath];

Return img;

}

// Check whether images exist locally

+ (BOOL) LocalHaveImage :( NSString *) key {

If ([JKBlankTool isBlankString: key]) {

Return NO;

}

// Read the local image instead of resource

NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];

NSLog (@ "check whether there are % @", picPath );

UIImage * img = [[UIImage alloc] initWithContentsOfFile: picPath];

If (img ){

Return YES;

}

Return NO;

}

// Delete the image from the local device

+ (Void) RemoveImageToLocalKeys :( NSString *) key {

NSString * picPath = [NSString stringWithFormat: @ "% @/Documents @.png", NSHomeDirectory (), key];

NSLog (@ "Delete the image from the local device % @", picPath );

[[NSFileManager defaultManager] removeItemAtPath: picPath error: nil];

}

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.