如何将我的Zip文件转换为NSData以作为附件发送我的Zip文件 [英] How can I convert my Zip-file to NSData to email my Zip file as an attachment

查看:200
本文介绍了如何将我的Zip文件转换为NSData以作为附件发送我的Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用目标Zip库来压缩我拍摄的几张图像。我来到这个点(我猜)我在哪里压缩一个图像。



现在我想用mailcomposer发送这个压缩文件。但是我需要在我的邮件功能中声明一个NSData对象。

  [picker addAttachmentData:NSData objectmimeType:@application / zipfileName:@test.zip]; 

这是我的代码片段

   - (IBAction)sendMail {
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];

NSString * path = [documentsDirectory stringByAppendingPathComponent:@test.zip];
NSArray * data = [[NSArray alloc] initWithObjects:@first,@second,@third,nil];

NSString * docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString * pngFilePath4 = [NSString stringWithFormat:@%@ / foto2.jpg,paths];
[data writeToFile:pngFilePath4 atomically:YES];

NSData * fotoData = [[NSData alloc] initWithContentsOfFile:pngFilePath4];

NSFileManager * manager = [[NSFileManager alloc] init];
[manager removeItemAtPath:pngFilePath4 error:nil];

ZipFile * readFile = [[ZipFile alloc] initWithFileName:路径模式:ZipFileModeCreate];

ZipWriteStream * stream = [readFile writeFileInZipWithName:@foto2.jpgcompressionLevel:ZipCompressionLevelNone];

[stream writeData:fotoData];
[stream finishedWriting];

MFMailComposeViewController * picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker addAttachmentData:NSData ObjectmimeType:@application / zipfileName:@test.zip];

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

[readFile close];
[data2 release];
[fotoData release];
}

我想我需要从readFile对象中创建另一个NSData对象,并将此在[picker attachmentData:method]中。希望有人能指出我正确的方向。



编辑



仍然无法使其正常工作。发送zip文件需要很长的时间(甚至通过wifi)。当我打开zip映像时,我收到一条错误,表示该文件无法打开。这是我的代码:

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

NSString * path = [documentsDirectory stringByAppendingPathComponent:@test.zip];
NSArray * data = [[NSArray alloc] initWithObjects:@first,@second,nil];

NSString * docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString * pngFilePath3 = [NSString stringWithFormat:@%@ / foto2.jpeg,docDir3];
NSData * imageData2 = [[[NSData alloc] initWithContentsOfFile:pngFilePath3] autorelease];

[data writeToFile:pngFilePath3 atomically:YES];

ZipFile * readFile = [[ZipFile alloc] initWithFileName:路径模式:ZipFileModeCreate];

ZipWriteStream * stream = [readFile writeFileInZipWithName:@foto2.jpegcompressionLevel:ZipCompressionLevelNone];

[stream writeData:imageData2];
[stream finishedWriting];


MFMailComposeViewController * picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker addAttachmentData:[NSData dataWithContentsOfFile:path] mimeType:@application / zipfileName:@test.zip];

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

[数据发布];
[readFile close];

提前感谢帮助我!

解决方案

ZipWriteStream 正在写入路径,并从文件路径使用 [NSData dataWithContentsOfFile:path] 完成。

 选择器addAttachmentData:[NSData dataWithContentsOfFile:path] 
mimeType:@application / zip
fileName:@test.zip];

当写入失败时,Objective-Zip会抛出异常,请确保添加一个try / catch写操作并确保您的数据不为零。


I', m using the Objective Zip library to compress several images i took. I came to the point (I guess) where I' zipping an image.

Now I'd like to send this zipped File with the mailcomposer. However I need to declare a "NSData object" within my mail function.

[picker addAttachmentData:"NSData object" mimeType:@"application/zip" fileName:@"test.zip"];

Here's a snippit of my code

-(IBAction)sendMail{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.zip"]; 
    NSArray *data = [[NSArray alloc] initWithObjects:@"first",@"second",@"third",nil];

    NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *pngFilePath4 = [NSString stringWithFormat:@"%@/foto2.jpg",paths];
    [data writeToFile:pngFilePath4 atomically:YES];

    NSData * fotoData = [[NSData alloc] initWithContentsOfFile:pngFilePath4];

    NSFileManager *manager = [[NSFileManager alloc] init]; 
    [manager removeItemAtPath:pngFilePath4 error:nil];

    ZipFile *readFile = [[ZipFile alloc] initWithFileName:path mode:ZipFileModeCreate];

    ZipWriteStream *stream = [readFile writeFileInZipWithName:@"foto2.jpg" compressionLevel:ZipCompressionLevelNone];

    [stream writeData:fotoData];
    [stream finishedWriting];

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate=self;

    [picker addAttachmentData:"NSData Object" mimeType:@"application/zip" fileName:@"test.zip"];

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

    [readFile close];
    [data2 release];
    [fotoData release];
}

I think i need to make another NSData object from the readFile object and place this within the [picker attachmentData: method]. Hope someone can point me in the right direction.

EDIT

Still can't get this to work properly. It takes realy long to send the zip file (Even through wifi). When I open the zip image I get an error which says that the file cannot be opened. Here's my code:

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

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.zip"]; 
    NSArray *data = [[NSArray alloc] initWithObjects:@"first",@"second", nil];

    NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *pngFilePath3 = [NSString stringWithFormat:@"%@/foto2.jpeg",docDir3];
    NSData * imageData2 = [[[NSData alloc] initWithContentsOfFile:pngFilePath3] autorelease];

    [data writeToFile:pngFilePath3 atomically:YES];

    ZipFile *readFile = [[ZipFile alloc] initWithFileName:path mode:ZipFileModeCreate];

    ZipWriteStream *stream = [readFile writeFileInZipWithName:@"foto2.jpeg" compressionLevel:ZipCompressionLevelNone];

    [stream writeData:imageData2];
    [stream finishedWriting];


    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate=self;
   [picker addAttachmentData:[NSData dataWithContentsOfFile:path] mimeType:@"application/zip" fileName:@"test.zip"];

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

    [data release];
    [readFile close];

Thanks in advance for helping me out!

解决方案

The ZipWriteStream is writing to path, and getting data from a file path is done using [NSData dataWithContentsOfFile:path].

[picker addAttachmentData:[NSData dataWithContentsOfFile:path] 
                 mimeType:@"application/zip" 
                 fileName:@"test.zip"];

Objective-Zip does throw exceptions when a write failure occurs so make sure you add a try/catch around the write operation and make sure your data is not nil.

这篇关于如何将我的Zip文件转换为NSData以作为附件发送我的Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆