IOS development-image compression/Decompression into Zip files

Source: Internet
Author: User
Tags ziparchive

IOS development-image compression/Decompression into Zip files
Compressing/decompressing images into Zip files

This article describes how to compress an image into a Zip file. First, you need to download the third-party library ZipArchive and import it to the project.

I. Document structure:
Ii. preparations:

1. Framework import:

2. The ZipArchive. m file uses a non-ARC mechanism.

Iii. Sample Code:

 

//// ViewController. m // UnzipImgDemo /// Created byLotheve on 15/4/10. // Copyright (c) 2015 Lotheve. all rights reserved. // # import ViewController. h # import ZipArchive. h @ interface ViewController () @ property (weak, nonatomic) IBOutlet UIImageView * imgView; @ property (weak, nonatomic) IBOutlet UIButton * zipBtn; @ property (weak, nonatomic) IBOutlet UIButton * unzipBtn; @ property (weak, nonatomic) IBOutlet UIButton * zipFileDeBtn; @ property (weak, nonatomic) IBOutlet UIButton * handle; @ property) IBOutlet UILabel * statusLabel; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self downLoadImg];}

 

// Load the image locally-(void) downLoadImg {NSString * URLString = @ brief NSURL * URL = [NSURL URLWithString: URLString]; NSData * data = [NSData dataWithContentsOfURL: URL]; NSMutableString * path = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) firstObject]; NSString * imgPath = [path paths: @baidu.png]; [data writeToFile: imgPath atomically: YES];}

 

// Compressed file-(IBAction) zipFile :( id) sender {NSString * docsPath = [encrypt (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * cachesPath = [decrypt (NSCachesDirectory, NSUserDomainMask, YES) firstObject]; NSString * imgPath = [cachesPath paths: @baidu.png]; NSString * zipFilePath = [docsPath paths: @newZipFile.zip]; // instantiate a compressed file, and create the file ZipArchive * za = [[ZipArchive alloc] init]; [za CreateZipFile2: zipFilePath]; // Add the file [za addFileToZip: imgPath newname: @baidu_zipped.png] In the compressed file; // disable the zip file operation BOOL success = [za CloseZipFile2]; if (success) {_ statusLabel. text = @ compressed successfully;} else {_ statusLabel. text = @ compression failed ;}}

 

// Extract the file-(IBAction) unzipFile :( id) sender {NSString * docsPath = [encrypt (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * zipPath = [docsPath stringByAppendingPathComponent: @ newzipfile.zip]; ZipArchive * za = [[ZipArchive alloc] init]; // decompress the file in the memory if ([za UnzipOpenFile: zipPath]) {// write the extracted content to the disk BOOL success = [za UnzipFileTo: docsPath overWrite: YES]; if (! Success) {_ statusLabel. text = @ decompression failed;} else {// close the compressed file [za UnzipCloseFile]; _ statusLabel. text = @ decompressed successfully; NSString * imgPath = [docsPath stringByAppendingPathComponent: Encrypted]; NSData * data = [NSData dataWithContentsOfFile: imgPath]; UIImage * image = [UIImage imageWithData: data]; _ imgView. image = image ;}} else {_ statusLabel. text = @ the compressed file does not exist ;}}

 

// Delete the compressed file-(IBAction) deleteZipFile :( id) sender {NSString * docsPath = [encrypt (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * zipPath = [docsPath stringByAppendingPathComponent: @ newzipfile.zip]; // create the File Manager NSFileManager * fm = [NSFileManager defaultManager]; // determine whether the specified path file contains BOOL exist = [fm fileExistsAtPath: zipPath]; if (exist) {NSError * error = nil; [fm RemoveItemAtPath: zipPath error: & error]; if (! Error) {_ statusLabel. text = @ compression File deleted successfully;} else {_ statusLabel. text = @ failed to delete the compressed file;} else {_ statusLabel. text = @ the file does not exist ;}}

 

// Delete the extracted file-(IBAction) deleteZipedFile :( id) sender {NSString * docsPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * zippedFilePath = [Folder: @ baidu_zipped.png]; NSFileManager * fm = [NSFileManager defaultManager]; BOOL exist = [fm fileExistsAtPath: zippedFilePath]; if (exist) {NSError * error = nil; [fm removeI TemAtPath: zippedFilePath error: & error]; if (! Error) {_ statusLabel. text = @ the decompressed file is successfully deleted; _ imgView. image = nil;} else {_ statusLabel. text = @ failed to delete the extracted file;} else {_ statusLabel. text = @ the file does not exist ;}}

 

@end

 

4. effect demonstration

Click the compression button:

 

Click the extract button:

 

Delete button:

 

 

 

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.