IOS開發—圖片壓縮/解壓成Zip檔案

來源:互聯網
上載者:User

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

 

四:效果示範

點擊壓縮按鈕:

 

點擊解壓按鈕:

 

刪除按鈕:

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.