IOS開發—圖片壓縮/解壓成Zip檔案
圖片壓縮/解壓成Zip檔案
本文介紹如何將圖片壓縮成Zip檔案,首先需要下載第三方庫ZipArchive 並匯入項目中。
一、文檔結構:
二、準備工作:
1、架構匯入:
2、ZipArchive.m檔案使用非ARC機制
三、程式碼範例:
//// 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 *zipedFiledeBtn; @property (weak, nonatomic) IBOutlet UILabel *statusLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self downLoadImg];}
//載入圖片到本地- (void)downLoadImg{ NSString *URLString = @https://www.baidu.com/img/bdlogo.png; NSURL *URL = [NSURL URLWithString:URLString]; NSData *data = [NSData dataWithContentsOfURL:URL]; NSMutableString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];; NSString *imgPath = [path stringByAppendingPathComponent:@baidu.png]; [data writeToFile:imgPath atomically:YES];}
//壓縮檔- (IBAction)zipFile:(id)sender { NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; NSString *imgPath = [cachesPath stringByAppendingPathComponent:@baidu.png]; NSString *zipFilePath = [docsPath stringByAppendingPathComponent:@newZipFile.zip]; //執行個體化一個壓縮文檔,並建立檔案 ZipArchive *za = [[ZipArchive alloc]init]; [za CreateZipFile2:zipFilePath]; //在壓縮文檔中添加檔案 [za addFileToZip:imgPath newname:@baidu_zipped.png]; //關閉zip檔案操作 BOOL success = [za CloseZipFile2]; if (success) { _statusLabel.text = @壓縮成功; }else{ _statusLabel.text = @壓縮失敗; }}
//解壓檔案- (IBAction)unzipFile:(id)sender { NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *zipPath = [docsPath stringByAppendingPathComponent:@newZipFile.zip]; ZipArchive *za = [[ZipArchive alloc]init]; //在記憶體中解壓檔案 if ([za UnzipOpenFile:zipPath]) { //將解壓的內容寫到磁碟中 BOOL success = [za UnzipFileTo:docsPath overWrite:YES]; if (!success) { _statusLabel.text = @解壓失敗; }else{ //關閉壓縮檔 [za UnzipCloseFile]; _statusLabel.text = @解壓成功; NSString *imgPath = [docsPath stringByAppendingPathComponent:@baidu_zipped.png]; NSData *data = [NSData dataWithContentsOfFile:imgPath]; UIImage *image = [UIImage imageWithData:data]; _imgView.image = image; } }else{ _statusLabel.text = @壓縮檔不存在; }}
//刪除壓縮檔- (IBAction)deleteZipFile:(id)sender { NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *zipPath = [docsPath stringByAppendingPathComponent:@newZipFile.zip]; //建立檔案管理工具 NSFileManager *fm = [NSFileManager defaultManager]; //判斷指定路徑檔案是否存在 BOOL exist = [fm fileExistsAtPath:zipPath]; if (exist) { NSError *error = nil; [fm removeItemAtPath:zipPath error:&error]; if (!error) { _statusLabel.text = @刪除壓縮檔成功; }else{ _statusLabel.text = @刪除壓縮檔失敗; } }else{ _statusLabel.text = @檔案不存在; }}
//刪除解壓檔案- (IBAction)deleteZipedFile:(id)sender { NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *zippedFilePath = [docsPathstringByAppendingPathComponent:@baidu_zipped.png]; NSFileManager *fm = [NSFileManager defaultManager]; BOOL exist = [fm fileExistsAtPath:zippedFilePath]; if (exist) { NSError *error = nil; [fm removeItemAtPath:zippedFilePath error:&error]; if (!error) { _statusLabel.text = @刪除解壓檔案成功; _imgView.image = nil; }else{ _statusLabel.text = @刪除解壓檔案失敗; } }else{ _statusLabel.text = @檔案不存在; }}
@end
四:效果示範
點擊壓縮按鈕:
點擊解壓按鈕:
刪除按鈕: