Retrieve saved image from app and email
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 w开发者_如何学Pythonhat the user did with your email sheet
//how would i attach the saved image from above?
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];
}
}
精彩评论