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

问题描述:

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

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

我成功地将我的令牌照片转换为NSData对象。现在我想压缩他们到一个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]; 
    } 
} 

希望有人能帮助我。

我找到的最好的zip库是SSZipArchive。给那个一个镜头。

+0

感谢您的评论。然而,在这个库的自述文件中说'目前它只支持解压缩。未来,将会支持创建zip文件。“所以我不认为这个库对我来说是非常有用的。 – BarryK88

我知道这是一个古老的线程,但在这里就是我拉上使用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; 
} 
+0

谢谢,帮了很多。 – NMunro

的NSString * TEMP = [[[makeZip textFieldAtIndex:0]文] stringByAppendingPathExtension:@”压缩”];

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