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

Posted

技术标签:

【中文标题】从应用程序和电子邮件中检索保存的图像【英文标题】:Retrieve saved image from app and email 【发布时间】:2011-05-17 01:05:22 【问题描述】:

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

- (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】:

这包括 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];
    

【讨论】:

还没有机会测试此代码,但这应该会引导您朝着正确的方向前进。不太确定您的“app.aImage2”是什么(我假设它是您在某处声明的 UIImage ivar)。 是的,抱歉忘记在我的应用程序委托中添加“app.aImage2”是一个全局 UIImage。这行得通!很明显,我必须对其进行一些编辑,但谢谢! 这段代码的大部分似乎是基于 Mike 在此处提供的代码:***.com/questions/1527351/…,因此最好给出适当的引用。 另外,其他部分来自 Sagar Kothari 的回答:***.com/questions/3289195/…

以上是关于从应用程序和电子邮件中检索保存的图像的主要内容,如果未能解决你的问题,请参考以下文章

从 iOS 上的 UIView 将图像保存到应用程序文档文件夹

如何从 iPhone 中的目录中检索图像?

在 ios 14 快捷方式中自动将图像从电子邮件保存到相册

使用swift在iOS上本地保存和检索包含所有元数据的照片

想将指纹图像保存在数据库中,也想检索相同的图像

使用linq mvc c#从数据库中检索图像