问题
I am trying to attach images to the email and send the email to my email add. The problem is that when i send out an email with 4 or 5 images attached, the app keeps processing for ever and eventually gets hanged and crashes and doesn't send the email. It is working fine with one image. I am thinking it is because of the size of the images combined together. Btw, I am using iOS 6.. How do i restrict the size of the files or images sent? Or there might be other issues involved? The same app is working in ios5....
The email sending portion together with image is:
for (int nCtr = 0; nCtr < [Pix count]; nCtr++) {
UIImageView *imageV = [Pix objectAtIndex:nCtr];
if (imageV.image) {
NSData *imageData = UIImagePNGRepresentation(imageV.image);
NSString *strFileName = [NSString stringWithFormat:@"MyPicture-%d.jpeg",nCtr];
NSString *strFormat = [NSString stringWithFormat:@"image/jpeg;\r\n\tx-unix-mode=0644;\r\n\tname=\"%@\"",strFileName];
NSString *strFormat2 = [NSString stringWithFormat:@"attachment;\r\n\tfilename=\"%@\"",strFileName];
NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:strFormat,kSKPSMTPPartContentTypeKey,
strFormat2,kSKPSMTPPartContentDispositionKey,[imageData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
[images addObject:vcfPart];
}
回答1:
There is something wrong in your code i can't figure but you can use this project it is
work will with multi attach file and it handle all cases.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(@"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}
回答2:
Just change it from PNG format to JPEG format.
for (int nCtr = 0; nCtr < [arrPix count]; nCtr++) {
UIImageView *imageV = [arrPix objectAtIndex:nCtr];
if (imageV.image) {
NSData *imageData = UIImageJPEGRepresentation(imageV.image, 0.9);
NSString *strFileName = [NSString stringWithFormat:@"MyPicture-%d.jpeg",nCtr];
NSString *strFormat = [NSString stringWithFormat:@"image/jpeg;\r\n\tx-unix-mode=0644;\r\n\tname=\"%@\"",strFileName];
NSString *strFormat2 = [NSString stringWithFormat:@"attachment;\r\n\tfilename=\"%@\"",strFileName];
NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:strFormat,kSKPSMTPPartContentTypeKey,
strFormat2,kSKPSMTPPartContentDispositionKey,[imageData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
[parts addObject:vcfPart];
}
}
It seems that ios6 limits the size of the images...Hence it is better to compress the image...
来源:https://stackoverflow.com/questions/13304371/attaching-a-image-to-an-email-in-an-app-in-ipad-in-ios6