1

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!

BarryK88
  • 1,806
  • 2
  • 25
  • 41

1 Answers1

2

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.

Joe
  • 56,979
  • 9
  • 128
  • 135