iOS Development-image compression/decompression into zip file

Source: Internet
Author: User
Tags ziparchive

Compress/Unzip the image into a zip file

This article describes how to compress a picture into a zip file, first you need to download the third-party library ziparchive and import the project.

Ziparchive Library Address: https://github.com/mattconnolly/ZipArchive

First, the document structure:
Ii. Preparatory work:

1. Frame Import:

2. ziparchive.m file using non-arc mechanism


Three, code example:

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, non Atomic) 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];}

Load picture to local-(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];}

Compressed file-(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"];    Instantiate a compressed document and create a file    ziparchive *za = [[ziparchive alloc]init];    [Za createzipfile2:zipfilepath];    Add a file in a compressed document    [za addfiletozip:imgpath newname:@ "baidu_zipped.png"];    Close zip file operation    BOOL success = [Za CloseZipFile2];    if (success) {        _statuslabel.text = @ "compression succeeded";    } else{        _statuslabel.text = @ "compression failed";    }}

//Extract File-(ibaction) Unzipfile: (ID) Sender {nsstring *docspath = [    Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject];    NSString *zippath = [Docspath stringbyappendingpathcomponent:@ "Newzipfile.zip"];    Ziparchive *za = [[Ziparchive alloc]init]; Unzip the file in memory if ([za unzipopenfile:zippath]) {//writes the extracted content to the disk BOOL success = [Za unzipfileto:docspath overwr        Ite:yes];        if (!success) {_statuslabel.text = @ "Decompression failed";            }else{//close compressed files [za unzipclosefile];            _statuslabel.text = @ "unzip successfully";            NSString *imgpath = [Docspath stringbyappendingpathcomponent:@ "Baidu_zipped.png"];            NSData *data = [NSData Datawithcontentsoffile:imgpath];            UIImage *image = [UIImage imagewithdata:data];        _imgview.image = image;    }}else{_statuslabel.text = @ "The compressed file does not exist"; }}

Delete compressed file-(ibaction) Deletezipfile: (ID) Sender {    NSString *docspath = [Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES) firstobject];    NSString *zippath = [Docspath stringbyappendingpathcomponent:@ "Newzipfile.zip"];    Create File Manager    Nsfilemanager *fm = [Nsfilemanager Defaultmanager];    Determines if the specified path file exists    BOOL exist = [FM Fileexistsatpath:zippath];    if (exist) {        Nserror *error = nil;        [FM Removeitematpath:zippath error:&error];        if (!error) {            _statuslabel.text = @ "Delete compressed file succeeded";        } else{            _statuslabel.text = @ "Delete compressed file failed";        }    } else{        _statuslabel.text = @ "file does not exist";    }}

Delete extract file-(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 = @ "Delete unzipped file succeeded";            _imgview.image = nil;        } else{            _statuslabel.text = @ "Delete unzip file failed";        }    } else{        _statuslabel.text = @ "file does not exist";    }}

@end

Four: Effect demonstration

Click the Compress button:


Click the Unzip button:


Delete button:


Attached: File please view in sandbox


iOS Development-image compression/decompression into zip file

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.