如何在我的应用程序中压缩多个图像

时间:2011-10-14 16:22:45

标签: iphone image compression zip send

在我的应用程序中,我正在使用相机拍摄几张照片。这些图片需要一次发送。出于这个原因,我正在尝试实现一个函数来压缩我拍摄的照片。

我一直在网上寻找我可以在我的项目中使用的几个库。我推出了Objective Zip(http://code.google.com/p/objective-zip/wiki/GettingStarted)。然而,我唯一成功的是压缩(或解压缩)文本文件。但是没有提到压缩(或解压缩)照片。

我已成功将令牌照片转换为NSData对象。现在我想将它们压缩为Zip文件。有没有人有过这些功能的经验。

非常感谢帮助!

修改

我现在正在使用Objective Zip库,但我的应用程序一直在崩溃。仍然得到已知错误“线程1:程序接收信号:”SIGARBT“”。我正在尝试将我用相机拍摄的一张图像压缩并存储在文档目录中。

在该片段中,您可以看到我正在调用我拍摄的照片并使用ZipFile方法进行压缩。最后,我将我的zip文件作为电子邮件附件发送。

以下是我的代码片段:

-(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];
    }
}

希望任何人都可以帮助我。

3 个答案:

答案 0 :(得分:2)

我知道这是一个旧线程,但这是我如何使用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;
}

答案 1 :(得分:1)

我找到的最好的zip库是SSZipArchive。给那个人一个机会。

答案 2 :(得分:0)

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];
        }
相关问题