Pregunta

I am using the MessageUI framework to send and image via e-mail after taking the photo using UIImagePickerController. When I take the photo and then invoke the mail mail message interface I get the compose window. When testing on an iPod touch (iOS 4.3.5) and iPad (iOS 5.0.1) I see the image attachment in the body of the compose window. When testing on an iPhone (4S iOS 5.0.1) the image does not appear in the compose window, but rather I see a box the size of the image attachment with an embedded small blue box with a "?" in it.

In both cases when the mail message is sent the image appears in the message received in the Mail app - iOS devices and Mac.

What can I do to fix this?

UPDATE: I've worked around this by changing:

NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(imageToSend)]

to:

NSData *imageDataJPG = [NSData dataWithData:UIImageJPEGRepresentation(imageToSend, 1.0)];

I can't see in the UIKit docs that there is anything in UIImagePNGRepresentation that would not work on an iPhone ...

(Xcode 4.2.1, ARC, 5.0 SDK, Deploy target 4.3)

Here is the code for composing the message:

-(void)displayComposerSheet { MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; mailPicker.mailComposeDelegate = self;

[mailPicker setSubject:@"Photo"];

NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(imageToSend)];
[mailPicker addAttachmentData:imageData mimeType:@"image/png" fileName:@"Photo"];


// Fill out the email body text.

NSString *line1 = [NSString stringWithFormat: @"I took this photo on my %@.\n", [[UIDevice currentDevice] model]];


NSString *body = [NSString stringWithFormat:@"%@", line1];
[mailPicker setMessageBody:body isHTML:NO];

// Present the mail composition interface.
[self presentModalViewController:mailPicker animated:YES];

}

¿Fue útil?

Solución

The image size is restricted so if the image you are sending through is greater than a certain dimension you'll get the effect you describe above.

I had a look at my own code and saw I had

#define MAX_MAIL_DIM 1536

Which seems to be 1024 * 1.5. Sorry I can't remember how I arrived at that number but I suspect it was trial and error.

Otros consejos

larik, your suggestion about using JPEG for the data type worked great. PNG files at this size are way too big anyway -- around 10MB. Here's the code with the JPEG NSData:

if ([MFMailComposeViewController canSendMail]) {
    picker = [[MFMailComposeViewController alloc] init];
    [picker setMailComposeDelegate:self];
    [picker setSubject:@"My Picture"];
    NSString *emailBody = @"";
    [picker setMessageBody:emailBody isHTML:YES];
    NSData *data = UIImageJPEGRepresentation(tempImage, 0);
    [picker addAttachmentData:data mimeType:@"image/jpg" fileName:@"CameraImage"];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top