3

I am succesfully saving an image to my app after the user takes a picture. What I want to do later is, when the user comes back to the app I want them to be able to email the photo as an attachment. I am not having any luck getting the data from the app converted to an image so I can add as an attachment. Can someone point me in the right direction please. Here is where I save the image after they have taken a picture.

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{


    //here is the image returned

        app.aImage2 = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *imageData = UIImagePNGRepresentation( app.aImage2 );
        NSString * savedImageName = [NSString stringWithFormat:@"r%@aImage2.png",app.reportNumber];
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentsDirectory = [paths objectAtIndex:0];
        NSString * dataFilePath;
        dataFilePath = [documentsDirectory stringByAppendingPathComponent:savedImageName];
        [imageData writeToFile:dataFilePath atomically:YES];



    [self dismissModalViewControllerAnimated:YES];


}

And here is where I need to attach it.

//this is inside my method that creates an email composer view
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the user did with your email sheet

    //how would i attach the saved image from above?
Louie
  • 5,920
  • 5
  • 31
  • 45

1 Answers1

3

This includes code that Mike mentions here:

How to add a UIImage in MailComposer Sheet of MFMailComposeViewController

Also, other portions are lifted from Sagar Kothari's answer here:

Sending out HTML email with IMG tag from an iPhone App using MFMailComposeViewController class

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Dismiss image picker modal.
    [picker dismissModalViewControllerAnimated:YES];

    if ([MFMailComposeViewController canSendMail]) {
        // Create a string with HTML formatting for the email body.
        NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"];

        // Add some text to it.
        [emailBody appendString:@"<p>Body text goes here.</p>"];

        // You could repeat here with more text or images, otherwise
        // close the HTML formatting.
        [emailBody appendString:@"</body></html>"];
        NSLog(@"%@", emailBody);

        // Create the mail composer window.
        MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
        emailDialog.mailComposeDelegate = self;

        // Image to insert.
        UIImage *emailImage = [info objectForKey:UIImagePickerControllerOriginalImage];

        if (emailImage != nil) {
            NSData *data = UIImagePNGRepresentation(emailImage);
            [emailDialog addAttachmentData:data mimeType:@"image/png" fileName:@"filename_goes_here.png"];
        }

        [emailDialog setSubject:@"Subject goes here."];
        [emailDialog setMessageBody:emailBody isHTML:YES];

        [self presentModalViewController:emailDialog animated:YES];
        [emailDialog release];
        [emailBody release];
    }
}
Community
  • 1
  • 1
gotnull
  • 26,454
  • 22
  • 137
  • 203
  • Haven't had a chance to test this code, but this should lead you in the right direction. Wasn't quite sure what your "app.aImage2" was (I'm assuming it's a UIImage ivar you've declared somewhere). – gotnull May 17 '11 at 06:11
  • Yeah sorry forgot to add that "app.aImage2" was a global UIImage in my app delegate. This works! I had to edit it a bit obviously but thanks! – Louie May 17 '11 at 15:57
  • Most of this code appears to be based on that provided by Mike here: http://stackoverflow.com/questions/1527351/how-to-add-an-uiimage-in-mailcomposer-sheet-of-mfmailcomposeviewcontroller-in-iph/2461451#2461451 , so it would be nice to give proper citation of this. – Brad Larson May 27 '11 at 16:27
  • Also, other portions are lifted from Sagar Kothari's answer here: http://stackoverflow.com/questions/3289195/sending-out-html-email-with-img-tag-from-an-iphone-app-using-mfmailcomposeviewcon/3290050#3290050 – Brad Larson May 27 '11 at 16:29