BarryK88
BarryK88

Reputation: 1806

How can I zip multiple images within my App

Within my App I'm using the Camera to take several pictures. These pictures need to be sended in one time. For that reason I'm trying to implement a function to compress the photo's I took.

I've been looking on the internet for several libraries which I could use within my project. I came out with Objective Zip (http://code.google.com/p/objective-zip/wiki/GettingStarted). However the only thing I succeeded was compressing (or decompressing) text-files. But compressing (or decompressing) photo's is not mentioned.

I've succeeded with converting my token photo's to a NSData object. Now i want to compress them to a Zip file. Is there anyone who have had experience with these kind of functions.

Help is greatly appreciated!

Edit

I'm now using the Objective Zip library but my App keeps crashing. Still getting the known error "Thread 1: Program reveived signal : "SIGARBT" ". I'm trying to zip one image I took with the camera and stored in the documents directory.

Within the snippet you can see that I'm calling the photo I took and zipping it with the ZipFile method. In the end I'm sending my zip-file as an email attachment.

Here's a snippet of my code:

-(IBAction)sendID{



    NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *docDir4 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *pngFilePath3 = [NSString stringWithFormat:@"%@/photo.png",docDir3];

    // Create the zip file
    ZipFile *zipFile = [[ZipFile alloc] initWithFileName:docDir4 mode:ZipFileModeCreate];

    // Read the files in the data directory
    NSError *error = nil;
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pngFilePath3 error:&error];

    //ZipWriteStream *stream1= [zipFile writeFileInZipWithName:@"abc.txt" fileDate:[NSDate //dateWithTimeIntervalSinceNow:-86400.0] compressionLevel:ZipCompressionLevelBest];


    // Write each file to our zip
    for (NSString *filename in files) {

        // Write the file
        ZipWriteStream *stream = [zipFile writeFileInZipWithName:filename compressionLevel:ZipCompressionLevelBest];

        NSData *data = [NSData dataWithContentsOfFile:pngFilePath3];

        [stream writeData:data];

        [stream finishedWriting];
    }

    // Close the zip file
    [zipFile close];

    NSData *data = [NSData dataWithContentsOfFile:@"Test.zip"];
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate=self;

    //NSData *imageData = UIImageJPEGRepresentation(viewImage, 1);

    [picker addAttachmentData:data mimeType:@"application/zip" fileName:@"Test.zip"];

    Class mailclass = (NSClassFromString(@"MFMailComposeViewController"));
    if([mailclass canSendMail]){
        [self presentModalViewController:picker animated:YES];
    }
}

Hope anyone can help me out.

Upvotes: 0

Views: 2162

Answers (3)

Aatish Javiya
Aatish Javiya

Reputation: 71

NSString *temp=[[[makeZip textFieldAtIndex:0]text]stringByAppendingPathExtension:@"zip"];

NSString *zippedPath = [pathString stringByAppendingPathComponent:temp];

NSMutableArray *inputPaths=[[NSMutableArray alloc]init];

    for (int i=0; i<[selectedRows count]; i++) {
                 [inputPaths addObject:[pathStringstringByAppendingPathComponent:[selectedRows objectAtIndex:i]]];
        }
        if (![[NSFileManager defaultManager] fileExistsAtPath:zippedPath])
        {
        [SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
            [directoryContents addObject:temp];
            [tblview reloadData];
        }

Upvotes: 0

DiscDev
DiscDev

Reputation: 39052

I know this is an old thread, but here's how I zip up multiple files using Objective-Zip:

- (NSString*) objectiveZip:(NSArray*)passedFiles
{
    NSString *outputPath = [self getOutputDirectory];

    NSString *zipFileName = @"MyZip.zip";

    outputPath = [outputPath stringByAppendingString:zipFileName];

    //Create a zip file for writing
    ZipFile *zipFile= [[ZipFile alloc] initWithFileName:outputPath mode:ZipFileModeCreate];

    //Add all the files, write to its stream and close it
    for(NSString *string in passedFiles)
    {
        NSString *fileName = string;

        NSLog(@"Add file to zip: %@", fileName);

        ZipWriteStream *stream1= [zipFile writeFileInZipWithName:fileName fileDate:[NSDate dateWithTimeIntervalSinceNow:-86400.0] compressionLevel:ZipCompressionLevelBest];

        [stream1 writeData:[NSData dataWithContentsOfURL:[NSURL URLWithString:info.pdfUrl]]];
        [stream1 finishedWriting];
    }

    // Close the zip file
    [zipFile close];

    return outputPath;
}

Upvotes: 2

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

The best zip lib I've found is SSZipArchive. Give that one a shot.

Upvotes: 1

Related Questions