我是,使用Objective Zip库来压缩我拍摄的几张图片。我来到了(我猜)我要压缩图像的地方。
现在我想用mailcomposer发送这个压缩文件。但是我需要在我的邮件功能中声明一个“NSData对象”。
[picker addAttachmentData:"NSData object" mimeType:@"application/zip" fileName:@"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: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];
}
我想我需要从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: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];
先谢谢你帮助我!
答案 0 :(得分:2)
ZipWriteStream
正在写入path
,并且使用[NSData dataWithContentsOfFile:path]
从文件路径获取数据。
[picker addAttachmentData:[NSData dataWithContentsOfFile:path]
mimeType:@"application/zip"
fileName:@"test.zip"];
当写入失败时,Objective-Zip会抛出异常,因此请确保在写入操作周围添加try / catch并确保数据不是nil。