从应用和电子邮件中检索已保存的图像

时间:2011-05-17 01:05:22

标签: iphone ios4 iphone-sdk-3.0

用户拍照后,我成功将图像保存到我的应用程序。我想要做的是,当用户回到应用程序时,我希望他们能够将照片作为附件发送。我没有运气将应用程序中的数据转换为图像,因此我可以添加为附件。请有人指出我正确的方向。这是我拍照后保存图像的地方。

- (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];


}

这是我需要附加它的地方。

//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?

1 个答案:

答案 0 :(得分:3)

这包括Mike在这里提到的代码:

How to add a UIImage in MailComposer Sheet of MFMailComposeViewController

此外,Sagar Kothari在这里的回答中提到了其他部分:

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];
    }
}