【问题标题】:How can I zip multiple images within my App如何在我的应用程序中压缩多个图像
【发布时间】:2011-12-07 21:47:38
【问题描述】:

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

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

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

非常感谢您的帮助!

编辑

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

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

这是我的代码的 sn-p:

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

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

【问题讨论】:

    标签: iphone image compression zip send


    【解决方案1】:

    我发现的最好的 zip 库是 SSZipArchive。试一试。

    【讨论】:

    • 感谢您的评论。然而,在这个库的自述文件中说“目前它只支持解压缩。将来,将支持创建 zip 文件。所以我不认为这个库对我很有用。
    【解决方案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;
    }
    

    【讨论】:

    • 谢谢,帮了大忙。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多