Julian F. Weinert
Julian F. Weinert

Reputation: 7560

Calculate mail attachment size in iOS

I'm trying to calculate the size of a mail created via MFMailComposeViewController. The user selects some titles from a UITableView and my app fetches appropriate PDF files from a server and attaches it to a new mail.

I'm now trying to calculate the attachment size (to show it or do some restrictions or whatever).

for(int i = 0; i < fileIDs.count; i++) {
    NSURL *docUrl = [NSURL URLWithString:[NSString stringWithFormat:
                                          @"http://domain.com/getPDF.php?fileID=%@", 
                                          [fileIDs objectAtIndex:i]]];

    NSData *docData = [NSData dataWithContentsOfURL:docUrl];

    [mailController addAttachmentData:docData 
                    mimeType:@"application/pdf" 
                    fileName:@"file.pdf"];

    mailLength = mailLength + [docData length];

    NSLog(@"Mail generation...\n%i of %i (%.2fMB)", 
          i+1, currentRows.count, ((float)(int)mailLength / 1048576));
}

I'm getting an output although. But it's much too large. The calculated file size is for example 5.14MB but the actual size is just 1.2MB.

What am I doing wrong?

Upvotes: 1

Views: 990

Answers (1)

Julian F. Weinert
Julian F. Weinert

Reputation: 7560

I fixed it.

Instead of using NSInteger I just had to use a float. It works right now:

// header:
float mailLength;

// implementation:
mailLength = mailLength + [docData length];
NSLog(@"Mail generation...\n%i of %i (%.2fMB)", i+1, currentRows.count, (mailLength / 1048576));

Upvotes: 2

Related Questions